Android 生成无限循环的压缩方法

Android 生成无限循环的压缩方法,android,zip,zipfile,Android,Zip,Zipfile,我正在尝试将文件夹的所有子文件夹放在zip文件中,因此我正在执行以下操作: public static void zipFolder(String inputFolderPath, String outZipPath) { try { FileOutputStream fos = new FileOutputStream(outZipPath); ZipOutputStream zos = new ZipOutputStream(f

我正在尝试将文件夹的所有子文件夹放在zip文件中,因此我正在执行以下操作:

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());
        }
publicstaticvoidzipfolder(stringinputfolderpath,stringoutzippath){
试一试{
FileOutputStream fos=新的FileOutputStream(outZipPath);
ZipoutStream zos=新ZipoutStream(fos);
File srcFile=新文件(inputFolderPath);
File[]files=srcFile.listFiles();
Log.d(“,”Zip目录:“+srcFile.getName());
对于(int i=0;i0){
写入(缓冲区,0,长度);
}
zos.closeEntry();
fis.close();
}
zos.close();
}捕获(ioe异常ioe){
Log.e(“,ioe.getMessage());
}
我在这里看到了这个代码

但是,代码进入循环,因为
lenght
总是1024

循环发生在第二个文件中。我打印了
文件列表

在本例中,我尝试创建“aaaaaaa”文件,其他文件来自另一次尝试,我不理解为什么会显示它们,因为目录中没有zip文件


PS:我正在尝试压缩的目录有两个子文件夹。我不知道这是否会影响。我解决了问题:D

在我的视野中,该方法不考虑子文件夹,只考虑文件。在我的例子中,在代码>文件\代码>目录和里面的内容中有两个文件夹。

因此,现在我调用每个子文件夹的方法,
outZipPath
应该是一个公共目录:

Controller.zipFolder(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath(), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/content_" +
                    Util.formatoData.format(data).replace("/", ".") + ".zip");
看看