我尝试将文件夹添加到zip文件,但总是得到ZipException
,表明zip文件小于预期大小。
val file = File.createTempFile("export_", ".zip")
val zipfile = ZipFile(file)
val tmpFolder = context.cacheDir.absolutePath + "/test"
File(tmpFolder).mkdir()
File(tmpFolder + "/test1.txt").createNewFile()
File(tmpFolder + "/test2.txt").createNewFile()
zipfile.addFolder(File(tmpFolder))
在最后一行,ZipException
被引发
E/AndroidRuntime: FATAL EXCEPTION: main
Process: xxxx, PID: 20023
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: net.lingala.zip4j.exception.ZipException: Zip file size less than minimum expected zip file size. Probably not a zip file or a corrupted zip file
at net.lingala.zip4j.headers.HeaderReader.readAllHeaders(HeaderReader.java:69)
at net.lingala.zip4j.ZipFile.readZipInfo(ZipFile.java:1142)
at net.lingala.zip4j.ZipFile.addFolder(ZipFile.java:378)
at net.lingala.zip4j.ZipFile.addFolder(ZipFile.java:365)
at net.lingala.zip4j.ZipFile.addFolder(ZipFile.java:331)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
文件和文件夹都存在,为什么会有例外?
2条答案
按热度按时间iibxawm41#
File.createTempFile("export_", ".zip")
在磁盘上创建一个文件,而File("somefile.zip")
只指向一个文件,但不创建它。zip4j
似乎检查该文件是否存在,如果它存在,它希望该文件是一个有效的zip文件。这就是为什么使用ZipFile(file)
失败,如果file
是用createTempFile()
创建的。owfi6suc2#
此问题已修复,请参阅the issue on GitHub。现在可以创建临时空存档。