[英]ZipEntry to File
Is there a direct way to unpack a java.util.zip.ZipEntry
to a File
?有没有直接的方法将
java.util.zip.ZipEntry
解压到File
?
I want to specify a location (like "C:\\temp\\myfile.java") and unpack the Entry to that location.我想指定一个位置(如“C:\\temp\\myfile.java”)并将 Entry 解压到该位置。
There is some code with streams on the net, but I would prefer a tested library function.网上有一些带有流的代码,但我更喜欢经过测试的库函数。
Use ZipFile class使用 ZipFile 类
ZipFile zf = new ZipFile("zipfile");
Get entry获取条目
ZipEntry e = zf.getEntry("name");
Get inpustream获取输入流
InputStream is = zf.getInputStream(e);
Save bytes节省字节
Files.copy(is, Paths.get("C:\\temp\\myfile.java"));
Use the below code to extract the "zip file" into File's then added in the list using ZipEntry
.使用以下代码将“zip 文件”解压缩到 File 中,然后使用
ZipEntry
添加到列表中。 Hopefully, this will help you.希望这会对您有所帮助。
private List<File> unzip(Resource resource) {
List<File> files = new ArrayList<>();
try {
ZipInputStream zin = new ZipInputStream(resource.getInputStream());
ZipEntry entry = null;
while((entry = zin.getNextEntry()) != null) {
File file = new File(entry.getName());
FileOutputStream os = new FileOutputStream(file);
for (int c = zin.read(); c != -1; c = zin.read()) {
os.write(c);
}
os.close();
files.add(file);
}
} catch (IOException e) {
log.error("Error while extract the zip: "+e);
}
return files;
}
Use ZipInputStream
to move to the desired ZipEntry
by iterating using the getNextEntry()
method.通过使用
getNextEntry()
方法进行迭代,使用ZipInputStream
移动到所需的ZipEntry
。 Then use the ZipInputStream.read(...)
method to read the bytes for the current ZipEntry
.然后使用
ZipInputStream.read(...)
方法读取当前ZipEntry
的字节。 Output those bytes to a FileOutputStream
pointing to a file of your choice.将这些字节输出到指向您选择的文件的
FileOutputStream
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.