1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
public static void testManifest(File file, String extractFolderPath) {
try {
InputStream in;
JarFile jarFile = new JarFile(file);
Enumeration<JarEntry> jarEntries = jarFile.entries();
// récupération de l'entrée du MANIFEST
JarEntry ze = jarFile.getJarEntry(JarFile.MANIFEST_NAME);
if (ze != null) {
in = jarFile.getInputStream(ze);
String outPath = extractFolderPath+"\\"+ze.toString();
String dirPath = extractFolderPath+"\\"+(new Path(ze.toString()).removeLastSegments(1)).toPortableString();
File dir = new File(dirPath);
File manifestFile = new File(outPath);
if (dir.mkdir()) {
if (!manifestFile.exists()) {
manifestFile.createNewFile();
}
}
manifestFile.setWritable(true);
FileOutputStream fo = new FileOutputStream(outPath, false);
byte[] buffer = new byte[IConstants.BUFFER_SIZE];
while (true) {
int readCount = in.read(buffer, 0, buffer.length);
if (readCount <= 0)
break;
fo.write(buffer, 0, readCount);
}
// traitement sur le MANIFEST
BufferedReader br = null;
BufferedWriter bw = null;
String line;
try
{
br = new BufferedReader(new FileReader(manifestFile));
}
catch(FileNotFoundException exc)
{
System.out.println("Erreur d'ouverture");
}
ArrayList<String> lines = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if (line.equals("\n")) {
System.out.println("j'ajoute la ligne");
lines.add("Test: 1\n");
}
lines.add(line);
}
br.close();
bw = new BufferedWriter(new FileWriter(manifestFile, false));
int cpt = 1;
for (String l : lines) {
try {
bw.write(l+"\n");
} catch (Exception e) {
e.printStackTrace();
}
cpt++;
}
bw.close();
long manifestCRC = 0;
CRC32 crc = new CRC32();
try {
FileInputStream fis = new FileInputStream(manifestFile);
byte[] buf = new byte[IConstants.BUFFER_SIZE];
int nb;
// on les rempli
while ((nb = fis.read(buf)) != -1)
crc.update(buf, 0, nb);
// on prend leur valeur
manifestCRC = crc.getValue();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = new FileOutputStream(file, true);
JarOutputStream jarOutputStream = new JarOutputStream(fos);
byte buffer[] = new byte[IConstants.BUFFER_SIZE];
CRC32 crc = new CRC32();
// Si le fichier pose problème, on le passe
if (!(srcFile == null || !srcFile.exists() || srcFile.isDirectory())) {
crc.reset();
// Calcul du CRC
FileInputStream fileInputStream = new FileInputStream(srcFile);
while (true) {
int readCount = fileInputStream.read(buffer, 0, buffer.length);
if (readCount <= 0)
break;
crc.update(buffer, 0, readCount);
}
fileInputStream.close();
// Ajoute une entrée à l'archive
JarEntry jarAdd = new JarEntry(projectRelativePath);
jarAdd.setMethod(JarEntry.STORED);
jarAdd.setCompressedSize(srcFile.length());
jarAdd.setSize(srcFile.length());
jarAdd.setCrc(crc.getValue());
jarOutputStream.putNextEntry(jarAdd);
// Ecrit dans l'archive
fileInputStream = new FileInputStream(srcFile);
while (true) {
int readCount = fileInputStream.read(buffer, 0, buffer.length);
if (readCount <= 0)
break;
jarOutputStream.write(buffer, 0, readCount);
}
fileInputStream.close();
}
JarEntry jarEntry = jarFile.getJarEntry(JarFile.MANIFEST_NAME);
if (jarEntry != null) {
jarEntry.setCrc(manifestCRC);
jarOutputStream.closeEntry();
jarOutputStream.flush();
}
jarOutputStream.closeEntry();
jarOutputStream.close();
fos.close();
fo.close();
if (in != null) {
in.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
} |
Partager