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
|
public static void deZipODT(String fileZip, boolean prems)
{
String cheminDossier = repertoire + "collectif/";
byte[] buffer = new byte[1024];
ZipInputStream zis;
try
{
File newFile = new File(cheminDossier);
newFile.mkdirs();
zis = new ZipInputStream(new FileInputStream(fileZip));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null)
{
if ("content.xml".equals(zipEntry.getName()))
{
String fileName = zipEntry.getName();
File content = new File(repertoire + fileName);
if (content.exists())
{
content.delete();
}
content.createNewFile();
FileOutputStream fos = new FileOutputStream(content);
int len;
while ((len = zis.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
fos.close();
}
else
{
if (prems)
{
String fileName = cheminDossier + zipEntry.getName();
if (zipEntry.isDirectory())
{
newFile = new File(fileName);
newFile.getParentFile().mkdirs();
newFile.mkdir();
}
else
{
newFile = new File(fileName);
newFile.getParentFile().mkdirs();
newFile.createNewFile();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0)
{
fos.write(buffer, 0, len);
}
fos.close();
}
}
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
catch (FileNotFoundException e)
{
System.out.println(fileZip + " fichier non trouvé");
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException
{
if (fileToZip.isDirectory())
{
File[] children = fileToZip.listFiles();
if (children.length == 0)
{
if (fileName.endsWith("/"))
{
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
}
else
{
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
zipOut.closeEntry();
}
}
else
{
for (File childFile : children)
{
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
}
}
return;
}
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileName.replace("collectif/", ""));
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0)
{
zipOut.write(bytes, 0, length);
}
fis.close();
}
private static void reZipODT()
{
int BUFFER_SIZE = 8 * 1024;
String dossier = repertoire + "collectif/";
FileOutputStream fos;
try
{
fos = new FileOutputStream(repertoire + "collectif.odt");
CheckedOutputStream checksum = new CheckedOutputStream(fos, new Adler32());
BufferedOutputStream bos = new BufferedOutputStream(checksum, BUFFER_SIZE);
ZipOutputStream zipOut = new ZipOutputStream(fos);
zipOut.setMethod(ZipOutputStream.DEFLATED);
zipOut.setLevel(Deflater.BEST_COMPRESSION);
File fileToZip = new File(dossier);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
bos.close();
checksum.close();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
} |
Partager