I am doing an Android project. I need to zip a complete folder structure of SD card. code is able to zip the folder if any files exists inside the folder, else skips that folder.
My Folder structure is like: mnt/sdcard/Movies/Telugu/Files.
mnt/sdcard/Movies/English --> English is empty folder
I did not see English folder in output zip file.
My code:
public void zip() {
try {
// create a ZipOutputStream to zip the data to
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
_zipFile));
zipDir(folderPath, zos);
// close the stream
zos.close();
} catch (Exception e) {
// handle exception
}
}
public void zipDir(String dir2zip, ZipOutputStream zos) {
try {
File zipDir = new File(dir2zip);
// get a listing of the directory content
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
// loop through dirList, and zip the files
for (int i = 0; i < dirList.length; i++) {
File f = new File(zipDir, dirList[i]);
if (f.isDirectory()) {
// if the File object is a directory, call this
// function again to add its content recursively
String filePath = f.getPath();
zipDir(filePath, zos);
// loop again
continue;
}
// if we reached here, the File object f was not a directory
// create a FileInputStream on top of f
FileInputStream fis = new FileInputStream(f);
// create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath());
// place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
// now write the content of the file to the ZipOutputStream
while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}
// close the Stream
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
// handle exception
}
}
Please help..