6

如果我的应用程序想要以动态方式使用 java 压缩结果文件(文件组),Java 中有哪些可用选项?当我浏览时,我有 java.util.zip 包可以使用,但是还有其他方法可以使用它来实现吗?

4

4 回答 4

15
public class FolderZiper {
  public static void main(String[] a) throws Exception {
    zipFolder("c:\\a", "c:\\a.zip");
  }

  static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
  }

  static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
      throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
      addFolderToZip(path, srcFile, zip);
    } else {
      byte[] buf = new byte[1024];
      int len;
      FileInputStream in = new FileInputStream(srcFile);
      zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
      while ((len = in.read(buf)) > 0) {
        zip.write(buf, 0, len);
      }
    }
  }

  static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
      throws Exception {
    File folder = new File(srcFolder);

    for (String fileName : folder.list()) {
      if (path.equals("")) {
        addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
      } else {
        addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
      }
    }
  }
}
于 2010-02-16T09:43:03.333 回答
3

已知原始 Java 实现存在一些与文件编码相关的错误。例如,它不能正确处理带有变音符号的文件名。

TrueZIP 是我们在项目中使用的替代方法:https ://truezip.dev.java.net/ 查看网站上的文档。

于 2010-02-16T09:46:05.573 回答
0

您可以使用 JDK 附带的 ZIP 文件处理库,教程也可能会有所帮助。

于 2010-02-16T09:41:33.850 回答
0

Java有一个 java.util.zip.ZipInputStream 并且你可以使用 ZipEntry ...类似的东西

public static void unZipIt(String zipFile, String outputFolder){
File folder = new File(zipFile);
    List<String> files = listFilesForFolder(folder);
    System.out.println("Size " + files.size());
    byte[] buffer = new byte[1024];
    try{
    Iterator<String> iter = files.iterator();
    while(iter.hasNext()){
        String file = iter.next();
    System.out.println("file name " + file);    
    ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
    ZipEntry ze = zis.getNextEntry();
    while(ze!=null){
           String fileName = ze.getName();
          File newFile = new File(outputFolder + File.separator + fileName);
          System.out.println("file unzip : "+ newFile.getAbsoluteFile());
           new File(newFile.getParent()).mkdirs();
           FileOutputStream fos = new FileOutputStream(newFile);             
           int len;
           while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
           }
           fos.close();   
           ze = zis.getNextEntry();
    }
    zis.closeEntry();
    zis.close();
    System.out.println("Done");
    }
   }catch(IOException ex){
      ex.printStackTrace(); 
   }
  }
于 2015-11-23T08:36:53.577 回答