I'm trying put all subfolders of a folder in a zip file, so I'm doing this:
public static void zipFolder(String inputFolderPath, String outZipPath) {
try {
FileOutputStream fos = new FileOutputStream(outZipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
File srcFile = new File(inputFolderPath);
File[] files = srcFile.listFiles();
Log.d("", "Zip directory: " + srcFile.getName());
for (int i = 0; i < files.length; i++) {
Log.d("", "Adding file: " + files[i].getName());
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
zos.close();
} catch (IOException ioe) {
Log.e("", ioe.getMessage());
}
I saw this code in this question
But, the code goes into loop because lenght
is always 1024
The loop happens at the second file. I printed the fileList
:
In this case I tried create the "aaaaaaa" file, the others are from another attempt and I didn't understand why they are showed, because the directory don't have none zip file.
PS: the directory that I'm trying compress has two subfolders. I don't know if this can influence.