如何将一个巨大的zip文件拆分成多个卷? [英] How to split a huge zip file into multiple volumes?

查看:1037
本文介绍了如何将一个巨大的zip文件拆分成多个卷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过 java.util.zip。* 创建zip存档时,有没有办法在多个卷中拆分生成的存档?

When I create a zip Archive via java.util.zip.*, is there a way to split the resulting archive in multiple volumes?

假设我的整体档案有一个 filesize 24 MB 我希望将其拆分为3个文件,每个文件的限制为10 MB。

是否有一个具有此功能的zip API?或者其他任何好方法来实现这个目标?

Let's say my overall archive has a filesize of 24 MB and I want to split it into 3 files on a limit of 10 MB per file.
Is there a zip API which has this feature? Or any other nice ways to achieve this?

谢谢
Thollsten

Thanks Thollsten

推荐答案

检查: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=38&t=004618

我不知道任何可以帮助您做到这一点的公共API。
(虽然如果你不想以编程方式进行,可以使用像WinSplitter这样的实用工具)

我有没试过但是,使用ZippedInput / OutputStream时每个ZipEntry都有一个压缩的大小。在创建压缩文件时,您可能会粗略估计压缩文件的大小。如果您需要2MB的压缩文件,那么在累积条目大小为1.9MB后,您可以停止写入文件,对于清单文件和其他特定于zip文件的元素占用.1MB。
因此,简而言之,您可以在ZippedInputStream上编写一个包装器,如下所示:

import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ChunkedZippedOutputStream {

    private ZipOutputStream zipOutputStream;

    private String path;
    private String name;

    private long currentSize;
    private int currentChunkIndex;
    private final long MAX_FILE_SIZE = 16000000; // Whatever size you want
    private final String PART_POSTFIX = ".part.";
    private final String FILE_EXTENSION = ".zip";

    public ChunkedZippedOutputStream(String path, String name) throws FileNotFoundException {
        this.path = path;
        this.name = name;
        constructNewStream();
    }

    public void addEntry(ZipEntry entry) throws IOException {
        long entrySize = entry.getCompressedSize();
        if((currentSize + entrySize) > MAX_FILE_SIZE) {
            closeStream();
            constructNewStream();
        } else {
            currentSize += entrySize;
            zipOutputStream.putNextEntry(entry);
        }
    }

    private void closeStream() throws IOException {
        zipOutputStream.close();
    }

    private void constructNewStream() throws FileNotFoundException {
        zipOutputStream = new ZipOutputStream(new FileOutputStream(new File(path, constructCurrentPartName())));
        currentChunkIndex++;
        currentSize = 0;
    }

    private String constructCurrentPartName() {
        // This will give names is the form of <file_name>.part.0.zip, <file_name>.part.1.zip, etc.
        StringBuilder partNameBuilder = new StringBuilder(name);
        partNameBuilder.append(PART_POSTFIX);
        partNameBuilder.append(currentChunkIndex);
        partNameBuilder.append(FILE_EXTENSION);
        return partNameBuilder.toString();
    }
}

上述程序只是一个提示方法,而不是任何方式的最终解决方案

这篇关于如何将一个巨大的zip文件拆分成多个卷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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