无法打开生成的 zip 文件 [英] Cannot open generated zip file

查看:86
本文介绍了无法打开生成的 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照几篇文章创建了一个 zip 文件使用 java ZipOutputStream 类.zip 已创建,但我无法打开它.在我的 Mac 上,当我使用 unzip 命令打开它时收到此消息:

I've followed several articles to create a zip file using java ZipOutputStream class. The zip is created but I cannot open it. On my Mac I'm receiving this message when I open it with the unzip command :

未找到中央目录结尾签名.要么这个文件不是一个 zip 文件,或者它构成一个多部分存档的磁盘.在里面后一种情况,中央目录和 zipfile 注释将在此存档的最后一个磁盘.

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.

解压缩:找不到压缩文件/Users/xxxx/Downloads/iad.zip 或
之一中的目录/Users/xxxx/Downloads/iad.zip.zip,找不到/Users/xxxx/Downloads/iad.zip.ZIP,句号.

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);
        }
    }
}

有没有人有打开这个 zip 的想法?

Doas anyone have an idea to get this zip open ?

推荐答案

您忘记调用 closeEntry().你应该在 baos.toByteArray() 之前为 ZipOutputStream 调用 close():

You forgot to call closeEntry(). And you should call close() for ZipOutputStream before baos.toByteArray():

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,您必须在从 ByteArrayOutputStream 检索字节数组之前关闭 ZipOutputStream.对于 FileOutputStream 也是一样的.您必须在关闭 FileOutputStream 之前关闭 ZipOutputStream.请注意,资源的 close 方法的调用顺序与其创建顺序相反.

For ByteArrayOutputStream you must close ZipOutputStream before retrieve byte array from ByteArrayOutputStream. For FileOutputStream is the same. You must close ZipOutputStream before closing FileOutputStream. Note that the close methods of resources are called in the opposite order of their creation.

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));
    }
}

这篇关于无法打开生成的 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆