【问题标题】:Split big zip file and combine it back as original zip file拆分大 zip 文件并将其合并为原始 zip 文件
【发布时间】:2020-05-14 08:43:00
【问题描述】:

我需要将 100mb zip 文件(将包含子文件夹和图像)拆分为 10 个 zip 文件(每个 10mb)。然后我需要将每个切片的 zip 文件发送到 API(作为多部分 reauest) ,在接收器 API 中,我需要将上述 10 个 zip 文件中的每一个组合回原始 100mb zip 文件。

下面是切片的代码

    public static void splitZip(String zipName, String location, String NewZip) throws IOException{
    FileInputStream fis  = new FileInputStream(location);
    ZipInputStream zipInputStream = new ZipInputStream(fis);
    ZipEntry entry = null;
    int currentChunkIndex = 0;
    long entrySize = 0;
    ZipFile zipFile = new ZipFile(location);
    Enumeration enumeration = zipFile.entries();
    String copDest = zipCopyDest + "\\" + NewZip + "_" + currentChunkIndex +".zip";
    FileOutputStream fos = new FileOutputStream(new File(copDest));
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    long currentSize = 0; 
    try {
        while ((entry = zipInputStream.getNextEntry()) != null && enumeration.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
            System.out.println(zipEntry.getName());
            System.out.println(zipEntry.getSize());
            entrySize = zipEntry.getSize();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            if((currentSize + entrySize) > MAX_FILE_SIZE) {
                zos.close();
                currentChunkIndex++;
                zos = getOutputStream(currentChunkIndex, NewZip);
                currentSize = 0;
            }else{
                currentSize += entrySize;
                zos.putNextEntry(new ZipEntry(entry.getName()));
                byte[] buffer = new byte[8192];
                int length = 0;
                while ((length = zipInputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, length);
                }
                byte[] unzippedFile = outputStream.toByteArray();
                zos.write(unzippedFile);
                unzippedFile = null;
                outputStream.close();
                zos.closeEntry();
            }
        }
    } finally {
        zos.close();
    }
}

当我手动提取切片拉链时,我发现一些图像已损坏无法打开。 也没有得到适当的解决方案来组合 zip 文件。提前致谢。

【问题讨论】:

  • 是否要求每个单独的文件都必须是有效的 ZIP 文件?因为否则只是将字节流分成 10 MB 块在概念上和代码方面要容易得多。
  • 一目了然,您似乎需要在两种情况下都执行else 部分(即是否切换到新的 ZIP 文件),否则您基本上会跳过每个发生的文件在 ZIP 文件边界处。
  • 否 .. 只有在合并所有单独的 zip 文件后才有效。
  • 在这种情况下,最简单的解决方案可能是忽略它是一个 ZIP 文件这一事实,只将 10MB 的块发送到服务器以接收,而根本不使用 Zip* 类。
  • 根据要求,我只需要使用 zip 文件..

标签: java spring-boot zip


【解决方案1】:

Zip 规范具有将 zip 文件拆分为任何所需长度的功能(最小拆分长度应为 64kb)。 Zip4j,支持此功能以创建拆​​分 zip 文件 (documentation)。然后,您可以将每个拆分文件传递给 api。然后,api 可以使用 zip4j 中的merge functionality 将这些拆分文件合并为单个 zip 文件。附带说明:即使没有合并,它也是一个完全有效的 zip 文件。只要所有拆分的 zip 文件都在同一个目录中。

【讨论】:

    【解决方案2】:

    如果 zip 中的任何文件大于 10 mb,则该方法将不起作用,并且在这两种情况下都应执行 else 条件,在这种方法中,您必须取决于 zip 中文件的大小可能会更好您采用不创建较小拉链的方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多