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
| private static void zipDir(String basedir, String currentdir, ZipOutputStream zos)
throws FileNotFoundException, IOException {
//create a new File object based on the directory we have to zip
File zipDir = (currentdir != null) ? new File(basedir, currentdir) : new File(basedir);
//get a listing of the directory content
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[BUFFER_SIZE];
int bytesIn = 0;
//On met pas File.separator, mais "/" parce que c'est le caractére utilisé
//dans les ZIP.
String currentdir2 = (currentdir != null) ? (currentdir + "/") : "";
File f;
FileInputStream fis;
BufferedInputStream bis;
ZipEntry anEntry;
//Create an empty entry with just dir name, like WinZip, so unzipToDir will
//behave correctly.
if (currentdir2.length() > 0) {
// anEntry = new ZipEntry(StringHelper.sansAccents(currentdir2));
anEntry = new ZipEntry(currentdir2);
zos.putNextEntry(anEntry);
zos.closeEntry();
}
//loop through dirList, and zip the files
for (int i = 0; i < dirList.length; i++) {
f = new File(zipDir, dirList[i]);
if (!f.exists())
continue;
if (f.isDirectory()) {
//if the File object is a directory, call this
//function again to add its content recursively
zipDir(basedir, currentdir2 + dirList[i], zos);
continue;
}
//if we reached here, the File object f was not a directory
//create a FileInputStream on top of f
fis = new FileInputStream(f);
try {
bis = new BufferedInputStream(fis, BUFFER_SIZE);
try {
//create a new zip entry
// anEntry = new ZipEntry(StringHelper.sansAccents(currentdir2 + dirList[i]));
anEntry = new ZipEntry(currentdir2 + dirList[i]);
anEntry.setTime(f.lastModified());
//place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
//now write the content of the file to the ZipOutputStream
while ((bytesIn = bis.read(readBuffer, 0, BUFFER_SIZE)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}
zos.closeEntry();
}
finally {
bis.close();
}
}
finally {
//close the Stream
fis.close();
}
}
} |
Partager