zib4j zipfile 在添加文件或文件夹时抛出 zip 文件大小小于最小值的 ZipException
zib4j zipfile throws ZipException for zip file size less than minimum when adding files or folders
我尝试将文件夹添加到 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.access00(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)
文件和文件夹都存在,为什么会出现异常?
File.createTempFile("export_", ".zip")
在磁盘上创建一个文件,而 File("somefile.zip")
只指向一个文件但不创建它。
zip4j
似乎在检查文件是否存在,如果存在,则认为该文件是有效的 zip 文件。这就是为什么如果 file
是用 createTempFile()
创建的,那么使用 ZipFile(file)
会失败。
我尝试将文件夹添加到 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.access00(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)
文件和文件夹都存在,为什么会出现异常?
File.createTempFile("export_", ".zip")
在磁盘上创建一个文件,而 File("somefile.zip")
只指向一个文件但不创建它。
zip4j
似乎在检查文件是否存在,如果存在,则认为该文件是有效的 zip 文件。这就是为什么如果 file
是用 createTempFile()
创建的,那么使用 ZipFile(file)
会失败。