使用 Java 压缩和解压缩文件夹和文件

发布于 2024-08-21 21:19:19 字数 104 浏览 7 评论 0原文

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

If My Application wants to zip the Resultant files(group of Files) using java in a dynamic way, what are the available options in Java?
When i Browsed I have got java.util.zip package to use, but is there any other way where I can use it to implement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人事已非 2024-08-28 21:19:20

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

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

The original Java implementation is known to have some bugs related to files encoding. For example it can't properly handle filenames with umlauts.

TrueZIP is an alternative that we've used in our project: https://truezip.dev.java.net/
Check the documentation on the site.

还给你自由 2024-08-28 21:19:20

可以使用JDK自带的ZIP文件处理库
另外教程可能会有所帮助。

You can use ZIP file handling library that comes with JDK
Also this tutorials might be helpful.

(り薆情海 2024-08-28 21:19:20

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

Java have a java.util.zip.ZipInputStream and along with this you can use ZipEntry ... Something like

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(); 
   }
  }
请你别敷衍 2024-08-28 21:19:19
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);
      }
    }
  }
}
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);
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文