0

我想要做的是压缩生成的 csv 文件。该文件确实可以毫无问题地生成,直到此处涉及此代码。所以代码在zos.close()这里抛出异常是代码

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);
}
4

1 回答 1

1

您在代码顶部创建fos和一次:zos

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

然后添加一个 ZipEntry:

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

然后稍后重新定义它们:

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

然后关闭新的 zos。你从来没有关闭过,也没有写信给第一个 zos(它有一个 ZipEntry),也从来没有向第二个(你试图在没有任何东西的情况下关闭)添加一个 ZipEntry。因此,至少一个 ZipEntry错误。

- - - - - - 编辑 - - - - - - -

尝试添加zos.finish(),另外,您的close()方法应该在 finally 块中......

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 ) {}
    }
}
于 2013-03-22T15:39:17.243 回答