java压缩文件夹的内容而不是文件夹& winzip兼容性

时间:2014-02-04 13:58:24

标签: java zip

更新

对于有关该文件夹的问题#2,我刚刚将ZipEntry ze = new ZipEntry(source + File.separator + file);替换为ZipEntry ze = new ZipEntry(file);

WINZIP 无法打开压缩文件,而解压缩可以解压缩文件时,

问题仍然存在。 WINZIP的错误是:Error: unable to seek to beginning of central directory.

原始帖子

我有以下代码,我从SO的一个问题中得到并略微修改。在我的应用程序中,我将OUTPUT_ZIP_FILE设置为/var/tmp/test/test.zip,将源文件夹设置为/var/tmp/test

我有两个问题:

1- unix解压缩时,Winzip无法识别zip文件 - 不确定这是否是由于#2下面的

2-当我使用解压缩解压缩文件时,它解压缩整个目录层次结构:它在/var/tmp/test内部创建/var/tmp/test,导致/var/tmp/test/var/tmp/test,然后在其中创建文件。我只想压缩文件而不是层次结构......

非常感谢任何帮助!

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

    public class ZipFiles {

List<String> fileList;
String OUTPUT_ZIP_FILE;
String SOURCE_FOLDER;

ZipFiles() {
    fileList = new ArrayList<String>();
}

public void zipIt(String ZipFiles) {
    byte[] buffer = new byte[1024];
    String source = "";
    Boolean shouldZip = true;
    try {
        try {
            // System.out.println("ZipFiles::zipIt::SOURCE_FOLDER::" +
            // SOURCE_FOLDER);

            source = SOURCE_FOLDER.substring(
                    SOURCE_FOLDER.lastIndexOf("\\") + 1,
                    SOURCE_FOLDER.length());
            // System.out.println("ZipFiles::zipIt::source::" + source);

        } catch (Exception e) {
            source = SOURCE_FOLDER;
        }
        for (String file : this.fileList) {
            if (file.endsWith("zip")) // This has already been zipped
            {
                shouldZip = false;
            }
        }
        if (shouldZip) {

            FileOutputStream fos = new FileOutputStream(ZipFiles);
            ZipOutputStream zos = new ZipOutputStream(fos);

            // System.out.println("Output to Zip : " + ZipFiles);

            for (String file : this.fileList) {

                // System.out.println("File Added : " + file);
                ZipEntry ze = new ZipEntry(source + File.separator + file);
                zos.putNextEntry(ze);

                FileInputStream in = new FileInputStream(SOURCE_FOLDER
                        + File.separator + file);

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

                in.close();
            }

            zos.closeEntry();
            // remember close it
            zos.close();
        }
        // System.out.println("Folder successfully compressed");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

public void generateFileList(File node) {

    // add file only
    if (node.isFile()) {
        fileList.add(generateZipEntry(node.toString()));

    }

    if (node.isDirectory()) {
        String[] subNote = node.list();
        for (String filename : subNote) {
            generateFileList(new File(node, filename));
        }
    }

}

private String generateZipEntry(String file) {
    // System.out.println("ZipFiles::generateZipEntry::file::" + file);
    return file.substring(SOURCE_FOLDER.length(), file.length());
}
}

1 个答案:

答案 0 :(得分:0)

正如我的问题更新中所述,对于问题#2:刚刚用ZipEntry ze = new ZipEntry(source + File.separator + file);替换ZipEntry ze = new ZipEntry(file);

对于使用winzip的问题#1,我测试的zip文件实际上是通过我的webapp下载的。问题实际上在我的代码的groovy方面:

  def download = {

   def folder = params.folder
   def file = new File( folder.toString())
   response.setHeader "Content-disposition", "attachment; filename=${file.name}"
   response.outputStream << file.text
   response.outputStream.flush()
   response.outputStream.close()
   }

当我用outputStream <<file.text替换outputStream << file.newInputStream()时,一切正常。 对于Unix'解压缩,我直接测试创建的文件而不是下载的文件!