Java ZIP - 如何解压缩文件夹?

问题描述:

是否有任何示例代码,如何将文件夹从ZIP解压到我想要的目录中?我已将文件夹“FOLDER”中的所有文件读入字节数组,我如何从其文件结构重新创建?Java ZIP - 如何解压缩文件夹?

您应该从zip文件中得到的所有条目:

Enumeration entries = zipFile.getEntries(); 

然后itareting在此枚举得到ZipEntry从它,检查它是否是一个目录或没有,创建dirctrory或只提取文件respectivly 。

+0

这是我真正需要的部分......我有机会到我的文件夹中ZIP,并希望将其存储在sdcard /文件夹名称中,其内容来自ZIP。怎么做? – Waypoint

+1

好吧,我认为你应该尝试编写一些代码,看看一些例子,如果你失败或陷入困境 - 用你的代码回来。 –

这是我正在使用的代码。根据需要更改BUFFER_SIZE。

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

你不应该吞下IOException。 –

+0

它适合我。谢谢。 –

使用Ant压缩库可以实现同样的效果。它将保留文件夹结构。

Maven的依赖性: -

<dependency> 
    <groupId>org.apache.ant</groupId> 
    <artifactId>ant-compress</artifactId> 
    <version>1.2</version> 
</dependency> 

示例代码: -

Unzip unzipper = new Unzip(); 
unzipper.setSrc(theZIPFile); 
unzipper.setDest(theTargetFolder); 
unzipper.execute(); 

我不知道你是什么意思particaly? 你的意思是在没有API帮助的情况下自己做吗?

在你不介意使用一些开源库的情况下, 存在应该是一个很酷的API在那里叫zip4J

它易于使用,我觉得这是它良好的反馈。 看到这个例子:

String source = "folder/source.zip"; 
String destination = "folder/source/"; 

try { 
    ZipFile zipFile = new ZipFile(source); 
    zipFile.extractAll(destination); 
} catch (ZipException e) { 
    e.printStackTrace(); 
} 

如果您要解压缩密码有文件,你可以试试这个:

String source = "folder/source.zip"; 
String destination = "folder/source/"; 
String password = "password"; 

try { 
    ZipFile zipFile = new ZipFile(source); 
    if (zipFile.isEncrypted()) { 
     zipFile.setPassword(password); 
    } 
    zipFile.extractAll(destination); 
} catch (ZipException e) { 
    e.printStackTrace(); 
} 

我希望这是有益的。

这是一个简单的解决方案,遵循更现代的约定。如果您要解压缩较大的文件,则可能需要将缓冲区大小更改为较小。这是因为你不保留所有的文件信息在内存中。

public static void unzip(File source, String out) throws IOException { 
    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) { 

     ZipEntry entry = zis.getNextEntry(); 

     while (entry != null) { 

      File file = new File(out, entry.getName()); 

      if (entry.isDirectory()) { 
       file.mkdirs(); 
      } else { 
       File parent = file.getParentFile(); 

       if (!parent.exists()) { 
        parent.mkdirs(); 
       } 

       try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) { 

        byte[] buffer = new byte[Math.toIntExact(entry.getSize())]; 

        int location; 

        while ((location = zis.read(buffer)) != -1) { 
         bos.write(buffer, 0, location); 
        } 
       } 
      } 
      entry = zis.getNextEntry(); 
     } 
    } 
} 

这是基于更“现代”的完整代码上this职位,但重构(使用Lombok):

import lombok.experimental.var; 
import lombok.val; 

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

import static java.nio.file.Files.createDirectories; 

public class UnZip 
{ 
    public static void unZip(String sourceZipFile, String outputDirectory) throws IOException 
    { 
     val folder = new File(outputDirectory); 
     createDirectories(folder.toPath()); 

     try (val zipInputStream = new ZipInputStream(new FileInputStream(sourceZipFile))) 
     { 
      var nextEntry = zipInputStream.getNextEntry(); 

      while (nextEntry != null) 
      { 
       val fileName = nextEntry.getName(); 
       val newFile = new File(outputDirectory + File.separator + fileName); 

       createDirectories(newFile.getParentFile().toPath()); 
       writeFile(zipInputStream, newFile); 

       nextEntry = zipInputStream.getNextEntry(); 
      } 

      zipInputStream.closeEntry(); 
     } 
    } 

    private static void writeFile(ZipInputStream inputStream, File file) throws IOException 
    { 
     val buffer = new byte[1024]; 
     try (val fileOutputStream = new FileOutputStream(file)) 
     { 
      int length; 
      while ((length = inputStream.read(buffer)) > 0) 
      { 
       fileOutputStream.write(buffer, 0, length); 
      } 
     } 
    } 
}