我正在尝试做的是压缩生成的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);
}

最佳答案

您只需在代码顶部创建一次foszos

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),也从来没有给第二个z​​os添加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 ) {}
    }
}

关于java - 使用Java压缩csv文件会抛出“至少一个ZipEntry”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15574175/

10-09 00:49