我尝试了java.util.zip包,但是速度太慢了。
然后我找到了LZMA SDK和7z jbinding,但它们都有缺陷。
LZMA SDK没有提供如何使用的教程或文档,这非常令人沮丧。没有javadoc。
而7z jbinding没有提供只提取一个文件的简单方法,它只提供提取zip文件所有内容的方法。此外,它也没有提供指定解压文件位置的方法。
有什么好的建议吗?
我尝试了java.util.zip包,但是速度太慢了。
然后我找到了LZMA SDK和7z jbinding,但它们都有缺陷。
LZMA SDK没有提供如何使用的教程或文档,这非常令人沮丧。没有javadoc。
而7z jbinding没有提供只提取一个文件的简单方法,它只提供提取zip文件所有内容的方法。此外,它也没有提供指定解压文件位置的方法。
有什么好的建议吗?
您的代码中使用了 java.util.zip
,请问您是处理多大的zip文件?并且可以提供一下您的代码吗?
我能够在大约一秒钟内使用以下代码,从一个包含1,800个条目的200MB zip文件中提取一个4MB的条目:
OutputStream out = new FileOutputStream("your.file");
FileInputStream fin = new FileInputStream("your.zip");
BufferedInputStream bin = new BufferedInputStream(fin);
ZipInputStream zin = new ZipInputStream(bin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.getName().equals("your.file")) {
byte[] buffer = new byte[8192];
int len;
while ((len = zin.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
break;
}
}
我没有对速度进行基准测试,但是在使用Java 7或更高版本时,我可以按照以下方式提取文件。
我想象它比ZipFile API更快:
从zip文件test.zip
中提取META-INF/MANIFEST.MF
的简短示例:
// file to extract from zip file
String file = "MANIFEST.MF";
// location to extract the file to
File outputLocation = new File("D:/temp/", file);
// path to the zip file
Path zipFile = Paths.get("D:/temp/test.zip");
// load zip file as filesystem
try (FileSystem fileSystem = FileSystems.newFileSystem(zipFile)) {
// copy file from zip file to output location
Path source = fileSystem.getPath("META-INF/" + file);
Files.copy(source, outputLocation.toPath());
}
JarFile
的文档中有),但应该使用随机访问文件操作来读取文件。由于 ZIP 文件包含一个已知位置的目录,这意味着要查找特定文件时需要进行较少的 IO 操作。try
/finally
确保在使用后关闭文件。
/* Relevant imports */
import com.google.protobuf.Message;
import com.google.protobuf.Parser;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public final class ZipFileUtils {
...
public static <T extends Message> Message readMessageFromZip(
final Path zipPath,
final Path entryPath,
final Parser<T> messageParser
) throws IOException {
try (ZipFile zipFile = new ZipFile(zipPath.toFile())) {
ZipEntry zipEntry = zipFile.getEntry(entryPath.toString());
return messageParser.parseFrom(zipFile.getInputStream(zipEntry));
}
}
}