2013-01-16 24 views
1

你好,我來自中國另一邊的太平洋。 請原諒我基本的英語。 國外網站的篩選導致更多問題。無法使用Zip4J在java中解壓縮

在我目前的項目中,我使用zip4j,但是出現了一個問題。這裏是我的代碼:

public static File [] unzip(File zipFile, String dest, String passwd) throws ZipException { 
    ZipFile zFile = new ZipFile(zipFile); 
    zFile.setFileNameCharset("GBK"); 
    if (!zFile.isValidZipFile()) { 
     throw new ZipException("壓縮文件不合法,可能被損壞."); 
    } 
    File destDir = new File(dest); 
    if (destDir.isDirectory() && !destDir.exists()) { 
     destDir.mkdir(); 
    } 
    if (zFile.isEncrypted()) { 
     zFile.setPassword(passwd.toCharArray()); 
    } 
    zFile.extractAll(dest); 

    List<FileHeader> headerList = zFile.getFileHeaders(); 
    List<File> extractedFileList = new ArrayList<File>(); 
    for(FileHeader fileHeader : headerList) { 
     if (!fileHeader.isDirectory()) { 
      extractedFileList.add(new File(destDir,fileHeader.getFileName())); 
     } 
    } 
    File [] extractedFiles = new File[extractedFileList.size()]; 
    extractedFileList.toArray(extractedFiles); 
    return extractedFiles; 
} 

後超過一定數量(我的文件2000)更壓縮包文件時出現的問題。 我使用了myeclipse開發工具和tomcat服務器。

我在myeclipse中使用了斷點,並沒有遇到任何錯誤。 沒有添加斷點,我直接操作錯誤:

net.lingala.zip4j.exception.ZipException: Compressed file is not valid, can be damaged. 
    at com.ninemax.cul.util.CompressUtil.unzip(CompressUtil.java:68) 
    at com.ninemax.cul.util.CompressUtil.unzip(CompressUtil.java:38) 
    at com.ninemax.cul.service.impl.SysShangChuanWenWuService.writeDataToDatabase(SysShangChuanWenWuService.java:42) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:311) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
    at $Proxy48.writeDataToDatabase(Unknown Source) 
    at com.ninemax.cul.action.web.UserUploadAction.uploadSuccess(UserUploadAction.java:93) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
+0

它是否始終是導致錯誤的文件?你的文件有多大可能是你的服務器內存達到了限制? – roel

+0

'com.ninemax.cul.util.CompressUtil.unzip(CompressUtil.java:68)':這一行是什麼?也如果它是Windows只可能有一些編碼問題?你如何閱讀/寫入文件?你在使用'PrintWriter'還是'PrintReader'?那些採取系統默認編碼。另外:'在Windows環境中出現錯誤' - 即使你正常解壓縮(使用Windows程序如7z)? –

回答

0

什麼是用於創建您要提取ZIP文件的工具?

很可能您的zip文件已損壞。嘗試使用Winzip(Windows)或GUnzip(UNIX)等解壓縮工具將其解壓縮,請檢查它是否損壞。

如果它沒有損壞,那麼很可能該zip是使用Zip4J不支持的加密方法加密和密碼保護的。例如Zip4J支持AES-256位Winzip加密/解密,但不支持AES-256 PKZIP加密/解密。

+0

文件沒有損壞,用其他解壓縮工具可以正常解壓縮,在Java測試類中也調用此方法也可以正常解壓縮,在Linux環境下項目可以解壓縮,但在Windows環境下出現錯誤。謝謝你的回答,再次期待你的回答 – user1982793