2013-03-22 16 views
0

我想要做的是壓縮生成的csv文件。直到涉及到此代碼時,文件纔會生成而不會出現問題。因此,代碼拋出這裏zos.close()異常的代碼使用Java壓縮csv文件會拋出一個「至少有一個ZipEntry」

try { 
    FileOutputStream fos = new FileOutputStream(file.getPath()); 
    ZipOutputStream zos = new ZipOutputStream(fos); 
    FileInputStream in = new FileInputStream(file.getPath()); 
    String fullCSVFileName = file.getName(); 
    String fullFileName = fullCSVFileName.substring(0, fullCSVFileName.length()-3); 
    String fullZipFileName = fullFileName + "zip"; 
    ZipEntry ze= new ZipEntry(fullZipFileName); 
    if(ze != null) zos.putNextEntry(ze); 
    fos = new FileOutputStream("C:\\sourceLocation\\"+fullZipFileName); 
    zos = new ZipOutputStream(fos); 

    byte[] buffer = new byte[1024]; 

    int len;// = in.read(buffer); 
    while ((len = in.read(buffer)) > 0) { 
     Logger.debug("in Loop, len = " + len); 
     zos.write(buffer, 0, len); 
    } 
    in.close(); 
    zos.closeEntry();  
    zos.close(); 

    Logger.debug("Zipping complete!"); 

} catch(IOException ex) { 
    Logger.error(ex); 
} 

更正後的代碼

try{ 

    String fullCSVFileName = file.getName(); 
    String fullFileName = fullCSVFileName.substring(0, fullCSVFileName.length()-3);      
    String fullZipFileName = fullFileName + "zip";      
    FileOutputStream fos = new FileOutputStream("C:\\sourceLocation\\"+fullZipFileName); 
    ZipOutputStream zos = new ZipOutputStream(fos); 
    FileInputStream in = new FileInputStream(file.getPath()); 

    ZipEntry ze= new ZipEntry(fullZipFileName); 
    if(ze != null){ 
     zos.putNextEntry(ze); 
    } 

    byte[] buffer = new byte[1024]; 

    int len;  
    while ((len = in.read(buffer)) > 0) { 
     zos.write(buffer, 0, len); 
    } 

    in.close(); 
    zos.closeEntry(); 
    zos.close(); 

    Logger.debug("Zipping complete!"); 

}catch(IOException ex){ 
    Logger.error(ex); 
} 
+0

你可以把你的堆棧跟蹤。 – commit 2013-03-22 15:55:41

+0

與'糾正的代碼'我得到一個zip文件那是在antoher zip文件中,它是'相同的名稱,它是'損壞或未知格式' – bouncingHippo 2013-03-22 16:13:11

回答

1

創建fos,並在你的代碼的頂部zos一次:

FileOutputStream fos = new FileOutputStream(file.getPath()); 
ZipOutputStream zos = new ZipOutputStream(fos); 

然後添加ZipEntry:

if(ze != null) zos.putNextEntry(ze); 

後來redifine他們:

fos = new FileOutputStream("C:\\sourceLocation\\"+fullZipFileName); 
zos = new ZipOutputStream(fos); 

然後關閉新ZOS。你永遠不會關閉,也不會寫信給第一個zos(它有一個ZipEntry),並且從不在第二個zz中添加一個ZipEntry(你試圖關閉它)。因此,至少有一個ZipEntry錯誤。

------------編輯--------------

嘗試增加zos.finish(),此外,您close()方法應該是在一個最後封鎖...

ZipOutputStream zos = null; 
FileInputStream in = null; 
try{ 

    String fullCSVFileName = file.getName(); 
    String fullFileName = fullCSVFileName.substring(0, fullCSVFileName.length()-3);      
    String fullZipFileName = fullFileName + "zip";      
    ZipOutputStream zos = new ZipOutputStream(
      new FileOutputStream("C:\\sourceLocation\\"+fullZipFileName)); 
    in = new FileInputStream(file.getPath()); 

    zos.putNextEntry(new ZipEntry(fullZipFileName)); 

    byte[] buffer = new byte[1024]; 

    int len;  
    while ((len = in.read(buffer)) > 0) { 
     zos.write(buffer, 0, len); 
    } 

    zos.finish(); 

    Logger.debug("Zipping complete!"); 

}catch(IOException ex){ 
    Logger.error(ex); 
}finally { 
    if (zos != null) { 
     try { 
      zos.close(); 
     } catch (Exception e) {} 
    } 
    if (in != null) { 
     try { 
      in.close(); 
     } catch (Exception e) {} 
    } 
} 
+0

我做了我的編輯更改,我能夠創建zip文件。然而,當我打開zipfile時,我看到另一個zipfile與它相同的名稱,並打開它給了我'檔案是未知格式或損壞' – bouncingHippo 2013-03-22 16:10:52

+0

@bouncingHippo,請參閱上面的編輯。 – Lucas 2013-03-22 17:31:14

+0

@boundingHippo,nevermind,完成實際上是由'close()'方法調用的... – Lucas 2013-03-22 17:35:43

相關問題