java - 无法打开生成的 zip 文件

标签 java zip

我关注了several使用 java ZipOutputStream 类创建 zip 文件的文章。 zip 已创建,但我无法打开它。在我的 Mac 上,当我使用 unzip 命令打开它时,我收到此消息:

End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive.

unzip: cannot find zipfile directory in one of /Users/xxxx/Downloads/iad.zip or
/Users/xxxx/Downloads/iad.zip.zip, and cannot find /Users/xxxx/Downloads/iad.zip.ZIP, period.

我的java类:

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static java.util.Arrays.stream;

@Slf4j
@UtilityClass
public class ZipCreator {

    public byte[] compressAll(String... files) throws IOException {

        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ZipOutputStream zipOut = new ZipOutputStream(baos)) {

            stream(files)
                    .forEach(file -> addToZip(zipOut, file));

            return baos.toByteArray();
        }
    }

    private static void addToZip(ZipOutputStream zipOut, String file) {
        File fileToZip = new File(file);
        try (FileInputStream fis = new FileInputStream(fileToZip.getCanonicalFile())) {
            zipOut.putNextEntry(new ZipEntry(fileToZip.getName()));

            byte[] bytes = new byte[1024];
            int length;
            while ((length = fis.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }
        } catch (IOException e) {
            log.error("Error when adding file {} to zip", file, e);
        }
    }
}

有人有办法打开这个 zipper 吗?

最佳答案

您忘记调用 closeEntry()。并且您应该在 baos.toByteArray() 之前调用 ZipOutputStream 的 close() :

public static byte[] compressAll(String... files) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ZipOutputStream zipOut = new ZipOutputStream(baos)) {
        stream(files).forEach(file -> addToZip(zipOut, file));
    }
    return baos.toByteArray();
}

private static void addToZip(ZipOutputStream zipOut, String file) {
    File fileToZip = new File(file);
    try (FileInputStream fis = new FileInputStream(fileToZip.getCanonicalFile())) {
        zipOut.putNextEntry(new ZipEntry(fileToZip.getName()));

        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zipOut.write(bytes, 0, length);
        }

        zipOut.closeEntry();
    } catch (IOException e) {
        log.error("Error when adding file {} to zip", file, e);
    }
}

对于 ByteArrayOutputStream,您必须先关闭 ZipOutputStream,然后才能从 ByteArrayOutputStream 检索字节数组。 对于FileOutputStream 来说也是一样。关闭 FileOutputStream 之前必须先关闭 ZipOutputStream。请注意,资源的 close 方法是按照与创建资源相反的顺序调用的。

public static void compressAll(String... files) throws IOException {
    try (FileOutputStream fos = new FileOutputStream("test.zip");
         ZipOutputStream zipOut = new ZipOutputStream(fos)) {
        stream(files).forEach(file -> addToZip(zipOut, file));
    }
}

关于java - 无法打开生成的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60745803/

相关文章:

java - 作业帮助 : List Constructors & Exceptions

java - 在通知点击时启动 Fragment 而不会丢失状态

linux - 满足特定名称标准的 tar 和 zip 文件的最佳方式?

java - 如何使用 AsyncTask 实时检索 RAM 消耗情况

java - 无法使用 Java(JDBC) 连接 Azure 数据库

java - JSONArray[0] 不是 JSONObject 异常

PHP zip 提取和重命名

python - 我可以编写包含临时文件的 .zip 吗?

java - 如何使用 KSOAP2 从 Web 服务获取文件的一部分并创建 zip?

Linux命令检查zip文件层次结构