Java 为什么压缩文件夹的代码只有在特定位置调用时才会创建一个不断增长的文件

Java 为什么压缩文件夹的代码只有在特定位置调用时才会创建一个不断增长的文件,java,android,Java,Android,我使用此代码创建应用程序数据文件夹内容的zip public static void zipFolder(File inputFolder) { try { File srcFile = inputFolder; File[] files = srcFile.listFiles(); FileOutputStream fos = new FileOutputStream(new File(inputFolder, "toSend.zip"

我使用此代码创建应用程序数据文件夹内容的zip

public static void zipFolder(File inputFolder) {
    try {
        File srcFile = inputFolder;
        File[] files = srcFile.listFiles();

        FileOutputStream fos = new FileOutputStream(new File(inputFolder, "toSend.zip"));
        ZipOutputStream zos = new ZipOutputStream(fos);

        Log.d("", "Zip directory: " + srcFile.getName());

        for (File file : files) {
            Log.d("", "Adding file: " + file.getName());
            byte[] buffer = new byte[1024];
            FileInputStream fis = new FileInputStream(file);
            zos.putNextEntry(new ZipEntry(file.getName()));
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();
            fis.close();
        }
        zos.close();
        fos.close();
    } catch (IOException ioe) {
        Log.e("", ioe.getMessage());
    }
}
当在常规代码中调用它时,它可以正常工作,但是当应用程序崩溃时,我希望在重写的Thread.UncaughtExceptionHandler中运行其他日志代码时运行它

在这种情况下,文件被创建,但它会无限期地增长,直到我强制关闭应用程序为止

当使用调试器遵循流程时,我看到正在压缩的文件(File[]文件)正是我所期望的,并且我成功地退出了函数调用(打开以执行以下代码),因此我认为函数不会陷入无休止的循环

public class MyApplication extends Application {

    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();

        //here and anywhere else in the code it manages to create a valid zip file with the folder contents
        File toZip = getExternalFilesDir(null);
        MyUtils.zipFolder(toZip);

        if (Costanti.SETTINGS_MAKE_CRASH_LOG)
            Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
            {
                @Override
                public void uncaughtException (Thread thread, Throwable e)
                {
                    e.printStackTrace();
                    File logFolder = getExternalFilesDir(null);

                    Log.d("Dump logcat from UncaughtExceptionHandler");
                    String filepath = getExternalFilesDir(null) + "/logcat.txt";
                    String cmd = "logcat -d -f "+filepath;
                    try {
                        Runtime.getRuntime().exec(cmd);
                    } catch (IOException ee) {
                        Log.d("Dump skipped");
                    }

                    StringWriter errors = new StringWriter();
                    e.printStackTrace(new PrintWriter(errors));

                    File file = new File(logFolder, "LOG-"+ MyUtils.getTSsemicompact());

                    try {
                        file.createNewFile();
                        FileOutputStream fOut = new FileOutputStream(file);
                        OutputStreamWriter myOutWriter =
                                new OutputStreamWriter(fOut);
                        myOutWriter.append(errors.toString());
                        myOutWriter.close();
                        fOut.close();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }

                    File toZip = getExternalFilesDir(null);
                    MyUtils.zipFolder(toZip);

                    //calling this because otherwise it would just stay stuck on the broken activity where the crash occurred
                    System.exit(0);//when debugging i can reach this line
                }
            });
    }

    public static Context getContext() {
        return mContext;
    }
}

你能在调用这个方法的地方发布代码吗?