java - 多次使用 ZipInputStream.getNextEntry() 时出现问题

标签 java android zip

我正在尝试从 zip 中解压缩特定文件。我首先得到一个ZipInputStream:

ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream(new FileInputStream(filePath)));

好的,这有效!现在我想提取两个文件,名为F1和F2,所以我调用

extractFileFromZip(zipIn, Path + "F1", "F1")
extractFileFromZip(zipIn, Path + "F2", "F2")

public static boolean extractFileFromZip(ZipInputStream inZip, String file, String name) throws Exception {
    byte data[] = new byte[BUFFER_SIZE];

    boolean found = false;

    ZipEntry ze;
    while((ze = inZip.getNextEntry()) != null) {
        if(ze.getName().equals(name)) {
            found = true;
            // delete old file first
            File oldFile = new File(file);
            if(oldFile.exists()) {
                if(!oldFile.delete()) {
                    throw new Exception("Could not delete " + file);
                }
            }

            FileOutputStream outFile = new FileOutputStream(file);
            int count = 0;
            while((count = inZip.read(data)) != -1) {
                outFile.write(data, 0, count);
            }

            outFile.close();
            //inZip.closeEntry();
        }
    }
    return true;
}

现在问题出在 inZip.getNextEntry() 上。对于 F1,它将正确循环遍历所有文件,然后给出 null。但对于 F2,它只会给出 null

为什么会发生这种情况?

最佳答案

您正在扫描整个流并消耗它。当您第二次尝试执行此操作时,流已经结束,因此不会执行任何操作。

此外,如果您只需要其中的一小部分,则流式传输 zip 文件中的所有字节会很慢。

使用 ZipFile相反,由于它允许随机访问 zip 条目,因此速度更快,并且允许以随机顺序读取条目。

注意:下面的代码已更改为使用 Java 7+ 功能,以便更好地处理错误,例如 try-with-resourcesNIO.2 .

ZipFile zipFile = new ZipFile(filePath);
extractFileFromZip(zipFile, path + "F1", "F1");
extractFileFromZip(zipFile, path + "F2", "F2");

public static boolean extractFileFromZip(ZipFile zipFile, String file, String name) throws IOException {
    ZipEntry ze = zipFile.getEntry(name);
    if (ze == null)
        return false;
    Path path = Paths.get(file);
    Files.deleteIfExists(path);
    try (InputStream in = zipFile.getInputStream(ze)) {
        Files.copy(in, path);
    }
    return true;
}

或者,仅流式传输一次,然后在 while 循环中检查两个名称。

Map<String, String> nameMap = new HashMap<>();
nameMap.put("F1", path + "F1");
nameMap.put("F2", path + "F2");
extractFilesFromZip(filePath, nameMap);

public static void extractFilesFromZip(String filePath, Map<String, String> nameMap) throws IOException {
    try (ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream(new FileInputStream(filePath)))) {
        for (ZipEntry ze; (ze = zipIn.getNextEntry()) != null; ) {
            String file = nameMap.get(ze.getName());
            if (file != null) {
                Path path = Paths.get(file);
                Files.deleteIfExists(path);
                Files.copy(zipIn, path);
            }
        }
    }
}

关于java - 多次使用 ZipInputStream.getNextEntry() 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910436/

相关文章:

java - JDBC中静态方法有什么好处?

android - onLongClick 时,AutoCompleteTextView 在剪切/复制/粘贴菜单上出现奇怪的裁剪问题

android - ActionBar 默认颜色

java - 如何知道 Firebase 列表是否无效?

linux - 如何获取 zip 中的目录列表?

java - 如何从 FileItem 获取完整路径

java - 获取 java.lang.NoClassDefFoundError : javax/naming/NamingException error when I access the OSGI bundle with JPA support

java - 在Android/java中求解两个方程组

java - SpotBugs 是否报告了未在此处关闭资源的误报?

Android studio 错误 : java. util.zip.ZipException: zip 文件为空