是否有任何示例代码,如何将文件夹从ZIP解压到我想要的目录中?我已将文件夹“FOLDER”中的所有文件读入字节数组,我如何从其文件结构重新创建?Java ZIP - 如何解压缩文件夹?
回答
您应该从zip文件中得到的所有条目:
Enumeration entries = zipFile.getEntries();
然后itareting在此枚举得到ZipEntry
从它,检查它是否是一个目录或没有,创建dirctrory或只提取文件respectivly 。
这是我正在使用的代码。根据需要更改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();
}
}
}
你不应该吞下IOException。 –
它适合我。谢谢。 –
使用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);
}
}
}
}
- 1. Windows压缩文件夹没有完全解压缩zip
- 2. 解压缩Zip文件3.6
- 3. 解压缩.zip文件
- 4. BlackBerry - 解压缩Zip文件
- 5. 解压缩.zip文件
- 6. Node.js - 压缩/解压缩文件夹
- 7. Android - 解压缩文件夹?
- 8. 如何使用rubyzip解压缩压缩文件夹
- 9. 如何使用Adobe Air下载和解压缩.ZIP文件夹?
- 10. 使用Java压缩和解压缩文件夹和文件
- 11. Java:解压缩文件夹与子文件夹
- 12. 如何在Silverlight中压缩和解压缩文件夹及其子文件夹?
- 13. 解压缩zip
- 14. 如何解压文件夹并删除压缩的原件?
- 15. VBScript解压缩zip文件的文件夹名称
- 16. 将zip文件解压缩到文件夹的根目录?
- 17. 创建zip文件的文件夹并解压缩到
- 18. Unix:递归解压缩各自文件夹中的.zip文件
- 19. java解压缩文件
- 20. 如何解压一个zip文件夹包含使用Java
- 21. 如何直接将csv文件压缩成zip压缩文件?
- 22. 如何使用zlib.h库将.zip文件解压缩到文件夹中?
- 23. 解压缩(解压缩)由.net压缩(解压缩)的文件.net system.io.compression.gzipstream
- 24. 如何在asp.net框架4.0中压缩/解压缩文件夹
- 25. 试图解压缩zipfile,然后在结果文件夹中找到并解压缩任何/所有zip文件
- 26. 如何使用VBScript将文件夹的每个子文件夹压缩为ZIP压缩文件?
- 27. C#解压缩后删除.ZIP文件
- 28. 在silverlight中解压缩zip文件
- 29. 解压缩文件夹并从解压缩文件中获取图像
- 30. 使用Java管理ZIP压缩文件
这是我真正需要的部分......我有机会到我的文件夹中ZIP,并希望将其存储在sdcard /文件夹名称中,其内容来自ZIP。怎么做? – Waypoint
好吧,我认为你应该尝试编写一些代码,看看一些例子,如果你失败或陷入困境 - 用你的代码回来。 –