使用java.util.zipfile解压同一层次结构中的zipfile

使用java.util.zipfile解压同一层次结构中的zipfile,java,unzip,Java,Unzip,给定一个具有多个嵌套目录结构的zip文件,如何将其解压缩到同一个树结构中? ZipFile.entries()是否以任何顺序提供枚举?为什么关心顺序 如果ZipFile条目有一个名称/a/b/c/file.txt,那么您可以计算出目录名/a/b/c,然后在树中创建一个名为a/b/cZipFile ZipFile=new ZipFile(“archive.zip”); ZipFile zipFile = new ZipFile("archive.zip"); try { for (Enumer

给定一个具有多个嵌套目录结构的zip文件,如何将其解压缩到同一个树结构中?
ZipFile.entries()是否以任何顺序提供枚举?

为什么关心顺序

如果ZipFile条目有一个名称
/a/b/c/file.txt
,那么您可以计算出目录名
/a/b/c
,然后在树中创建一个名为
a/b/c

ZipFile ZipFile=new ZipFile(“archive.zip”);
ZipFile zipFile = new ZipFile("archive.zip");
try {
  for (Enumeration<? extends ZipEntry> entries = zipFile.entries(); entries.hasMoreElements();) {
    ZipEntry entry = entries.nextElement();

    if (entry.isDirectory()) {
      new File(entry.getName()).mkdirs();
    } else {
      InputStream in = zipFile.getInputStream(entry);
      try {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(entry.getName()));
          try {
            // this util class is taken from apache commons io (see http://commons.apache.org/io/)
            IOUtils.copy(in, out);
          } finally {
            out.close();
          }
      } finally {
        in.close();
      }
    }
  }
} catch (IOException e) {
  e.printStackTrace();
} finally {
  zipFile.close();
}
试一试{
for(EnumerationZip本身不提供目录结构。树状结构是通过每个条目的完整路径构建的。ZipFile以与添加到文件中相同的方式枚举条目

注意:java.util.ZipEntry.isDirectory()只测试名称的最后一个字符是否为“/”,这就是它的工作原理

将文件解压缩到同一目录中所需的内容。请按以下方式解析文件名:

for(ZipEntry zipEntry : java.util.Collections.list(zipFile.entries())){//lazislav
    String name = zipEntry.getName();
    int idx = name.lastIndexOf('/');
    if (idx>=0) name=name.substring(idx)
    if (name.length()==0) continue;

    File f = new File(targetDir, name);

}
这或多或少可以做到(您仍然需要注意重复的文件名等)

这是我的

在文件中,指定要展开的文件 在目标目录中,必须将目标位置指定为“新文件(“/tmp/foo/bar”)。如果要在当前目录中提取,可以指定targetDir=new File(“.”)

publicstaticvoidunzip(File-File,File-targetDir)抛出ZipException,
IOException{
targetDir.mkdirs();
ZipFile ZipFile=新ZipFile(文件);
试一试{

枚举这是我一直使用的枚举。它应该在复制/粘贴后以及在任何情况下都能直接工作

     public static File unzip(File inFile, File outFolder)
 {  final int BUFFER = 2048;
      try
      {
           BufferedOutputStream out = null;
           ZipInputStream  in = new ZipInputStream(
                                         new BufferedInputStream(
                                              new FileInputStream(inFile)));
           ZipEntry entry;
           while((entry = in.getNextEntry()) != null)
           {
                //System.out.println("Extracting: " + entry);
                int count;
                byte data[] = new byte[BUFFER];

                //We will try to reconstruct the entry directories
                File entrySupposedPath = new File(outFolder.getAbsolutePath()+File.separator+entry.getName());

                //Does the parent folder exist?
                if (!entrySupposedPath.getParentFile().exists()){
                    entrySupposedPath.getParentFile().mkdirs();
                }


                // write the files to the disk
                out = new BufferedOutputStream(
                          new FileOutputStream(outFolder.getPath() + "/" + entry.getName()),BUFFER);

                while ((count = in.read(data,0,BUFFER)) != -1)
                {
                     out.write(data,0,count);
                }
                out.flush();
                out.close();
           }
           in.close();
           return outFolder;
      }
      catch(Exception e)
      {
           e.printStackTrace();
           return inFile;
      }
 }

很好的代码对我来说很好,并且保持了所有文件夹和zip中的结构与我需要的一样。实际上,在使用它一点之后,它对更大的文件不起作用。出于某种原因,我有一个105mbs的zip,它甚至没有开始解压。很有趣。你有什么例外吗?很好!保持相同的层次结构
     public static File unzip(File inFile, File outFolder)
 {  final int BUFFER = 2048;
      try
      {
           BufferedOutputStream out = null;
           ZipInputStream  in = new ZipInputStream(
                                         new BufferedInputStream(
                                              new FileInputStream(inFile)));
           ZipEntry entry;
           while((entry = in.getNextEntry()) != null)
           {
                //System.out.println("Extracting: " + entry);
                int count;
                byte data[] = new byte[BUFFER];

                //We will try to reconstruct the entry directories
                File entrySupposedPath = new File(outFolder.getAbsolutePath()+File.separator+entry.getName());

                //Does the parent folder exist?
                if (!entrySupposedPath.getParentFile().exists()){
                    entrySupposedPath.getParentFile().mkdirs();
                }


                // write the files to the disk
                out = new BufferedOutputStream(
                          new FileOutputStream(outFolder.getPath() + "/" + entry.getName()),BUFFER);

                while ((count = in.read(data,0,BUFFER)) != -1)
                {
                     out.write(data,0,count);
                }
                out.flush();
                out.close();
           }
           in.close();
           return outFolder;
      }
      catch(Exception e)
      {
           e.printStackTrace();
           return inFile;
      }
 }