簡體   English   中英

使用Java壓縮和解壓縮文件夾和文件

[英]Zip and Unzip the Folders and Files Using Java

如果我的應用程序想要以動態方式使用java壓縮Resultant文件(文件組),那么Java中有哪些可用選項? 當我瀏覽時,我已經使用了java.util.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);
      }
    }
  }
}

已知原始Java實現具有與文件編碼相關的一些錯誤。 例如,它無法使用變音符號正確處理文件名。

TrueZIP是我們在項目中使用的替代方案: https ://truezip.dev.java.net/查看網站上的文檔。

您可以使用JDK附帶的ZIP文件處理庫。 教程可能也會有所幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM