2

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:

enter image description here

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.

Arun J
  • 687
  • 4
  • 14
  • 27
Éowyn
  • 187
  • 3
  • 17

1 Answers1

0

I solved the problem :D

In my vision, the method don't consider subfolders, only files. In my case I have two folders inside files directory and the content inside them.

So, now I call the method for each subfolder and the outZipPath should be a public directory:

Controller.zipFolder(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath(), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/content_" +
                    Util.formatoData.format(data).replace("/", ".") + ".zip");
Éowyn
  • 187
  • 3
  • 17