有没有直接将java.util.zip.ZipEntry
解压到File
中的方法?
我想指定一个位置(比如“C:\temp\myfile.java”),然后将该Entry解压到该位置。
网络上有一些使用流的代码,但我更喜欢使用测试过的库函数。
ZipFile zf = new ZipFile("zipfile");
获取入口
ZipEntry e = zf.getEntry("name");
获得输入流
InputStream is = zf.getInputStream(e);
节省字节
Files.copy(is, Paths.get("C:\\temp\\myfile.java"));
ZipEntry
将其添加到列表中。希望这能帮到你。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;
}
ZipInputStream
通过迭代使用getNextEntry()
方法来移动到所需的ZipEntry
。然后使用ZipInputStream.read(...)
方法读取当前ZipEntry
的字节。将这些字节输出到指向您选择的文件的FileOutputStream
中。
File
?”File
是路径名的抽象表示。你是指“文件”(没有代码标记)吗? - T.J. Crowder