如何使用Java解压缩包含不同文件格式的zip文件夹

时间:2013-02-28 12:06:17

标签: java xml unzip

我需要解压缩包含不同文件格式的压缩目录,如.txt, .xml, .xls等。

如果目录只包含.txt files,我可以解压缩,但是它会以其他文件格式失败。下面是我正在使用的程序,经过一些谷歌搜索后,我看到的只是类似的方法 -

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipUtils {
  public static void extractFile(InputStream inStream, OutputStream outStream) throws IOException {
      byte[] buf = new byte[1024];
      int l;
      while ((l = inStream.read(buf)) >= 0) {
           outStream.write(buf, 0, l);
      }
      inStream.close();
      outStream.close();
  }

  public static void main(String[] args) {
      Enumeration enumEntries;
      ZipFile zip;

      try {
          zip = new ZipFile("myzip.zip");
          enumEntries = zip.entries();
          while (enumEntries.hasMoreElements()) {
              ZipEntry zipentry = (ZipEntry) enumEntries.nextElement();
              if (zipentry.isDirectory()) {
                  System.out.println("Name of Extract directory : " + zipentry.getName());
                  (new File(zipentry.getName())).mkdir();
                  continue;
              }
              System.out.println("Name of Extract fille : " + zipentry.getName());

              extractFile(zip.getInputStream(zipentry), new FileOutputStream(zipentry.getName()));
          }
          zip.close();
     } catch (IOException ioe) {
         System.out.println("There is an IoException Occured :" + ioe);
         ioe.printStackTrace();
     }
  }
}

引发以下异常 -

There is an IoException Occured :java.io.FileNotFoundException: myzip\abc.xml (The system cannot find the path specified)
java.io.FileNotFoundException: myzip\abc.xml (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
    at updaterunresults.ZipUtils.main(ZipUtils.java:43)

2 个答案:

答案 0 :(得分:3)

当您尝试打开包含提取内容的文件时,会发生错误。 这是因为myzip文件夹不可用。

因此检查它是否确实不可用并在解压缩zip之前创建它:

File outputDirectory = new File("myzip");
if(!outputDirectory.exists()){
    outputDirectory.mkdir();
}

注释中指出@ Perception:输出位置相对于活动/工作目录。这可能不太方便,因此您可能希望将提取位置添加到提取文件的位置:

File outputLocation = new File(outputDirectory, zipentry.getName());
extractFile(zip.getInputStream(zipentry), new FileOutputStream(outputLocation));

(当然您还需要将outputLocation添加到目录创建代码中)

答案 1 :(得分:2)

他展示解压缩所有格式(pdf,txt等)的

This is a good example看起来非常

或者您可以使用此代码可能有效(我还没试过)

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipUtils
{
  private static final int  BUFFER_SIZE = 4096;

  private static void extractFile(ZipInputStream in, File outdir, String name) throws IOException
  {
    byte[] buffer = new byte[BUFFER_SIZE];
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outdir,name)));
    int count = -1;
    while ((count = in.read(buffer)) != -1)
      out.write(buffer, 0, count);
    out.close();
  }

  private static void mkdirs(File outdir,String path)
  {
    File d = new File(outdir, path);
    if( !d.exists() )
      d.mkdirs();
  }

  private static String dirpart(String name)
  {
    int s = name.lastIndexOf( File.separatorChar );
    return s == -1 ? null : name.substring( 0, s );
  }

  /***
   * Extract zipfile to outdir with complete directory structure
   * @param zipfile Input .zip file
   * @param outdir Output directory
   */
  public static void extract(File zipfile, File outdir)
  {
    try
    {
      ZipInputStream zin = new ZipInputStream(new FileInputStream(zipfile));
      ZipEntry entry;
      String name, dir;
      while ((entry = zin.getNextEntry()) != null)
      {
        name = entry.getName();
        if( entry.isDirectory() )
        {
          mkdirs(outdir,name);
          continue;
        }
        /* this part is necessary because file entry can come before
         * directory entry where is file located
         * i.e.:
         *   /foo/foo.txt
         *   /foo/
         */
        dir = dirpart(name);
        if( dir != null )
          mkdirs(outdir,dir);

        extractFile(zin, outdir, name);
      }
      zin.close();
    } 
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

此致