経由で zip アーカイブを作成する場合java.util.zip.*、結果のアーカイブを複数のボリュームに分割する方法はありますか?
アーカイブ全体にfilesizeof が24 MBあり、ファイルごとに 10 MB の制限で 3 つのファイルに分割したいとします。
この機能を持つ zip API はありますか? またはこれを達成するための他の良い方法はありますか?
ありがとうトールステン
チェック: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=38&t=004618
それを行うのに役立つ公開 API については知りません。(ただし、プログラムで実行したくない場合は、WinSplitter などのユーティリティで実行できます)
私は試していませんが、ZippedInput/OutputStream を使用している間のすべての ZipEntry のサイズは圧縮されています。zip ファイルの作成中に、zip ファイルのサイズの概算が得られる場合があります。2MB の zip ファイルが必要な場合、エントリの累積サイズが 1.9MB になったら、ファイルへの書き込みを停止できます。マニフェスト ファイルとその他の zip ファイル固有の要素に .1MB を使用します。 したがって、簡単に言えば、次のように ZippedInputStream のラッパーを記述できます。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ChunkedZippedOutputStream {
private ZipOutputStream zipOutputStream;
private final String path;
private final 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.
return name + PART_POSTFIX + currentChunkIndex + FILE_EXTENSION;
}
}
上記のプログラムは、アプローチのヒントに過ぎず、決して最終的な解決策ではありません。
出力を pkzip および winzip と互換性があるようにすることが目標である場合、これを行うオープン ソース ライブラリを知りません。私たちのアプリの 1 つに同様の要件があり、独自の実装 (zip 標準と互換性があります) を作成することになりました。思い出すと、私たちにとって最も困難だったのは、個々のファイルをその場で生成しなければならなかったことです (ほとんどの zip ユーティリティは、大きな zip ファイルを作成し、後で戻って分割するという方法で動作します。これは、はるかに簡単です)書き込みに約 1 日、デバッグに 2 日かかりました。
zip 標準は、ファイル形式がどのように見える必要があるかを説明しています。少し袖をまくることを恐れないのであれば、これは間違いなく実行可能です。zip ファイル ジェネレーターを自分で実装する必要がありますが、Java の Deflator クラスを使用して、圧縮データのセグメント ストリームを生成できます。ファイルとセクション ヘッダーを自分で生成する必要がありますが、それらは単なるバイトです。
これがzip の仕様です - セクション K には特に探している情報がありますが、A、B、C、および F も読む必要があります。非常に大きなファイルを扱っている場合 (私たちはそうでした)、Zip64 も使用する必要がありますが、24 MB の場合は問題ありません。
飛び込んで試してみたい場合は、質問がある場合は、返信してください。いくつかのヒントを提供できるかどうかを確認します.
これが私の解決策です:
public abstract class ZipHelper {
public static NumberFormat formater = NumberFormat.getNumberInstance(new Locale("pt", "BR"));
public static List<Path> zip(Collection<File> inputFiles, long maxSize) throws IOException {
byte[] buffer = new byte[1024];
int count = 0;
long currentZipSize = maxSize;
List<Path> response = new ArrayList<>();
ZipOutputStream zip = null;
for (File currentFile : inputFiles) {
long nextFileSize = currentFile.length();
long predictedZipSize = currentZipSize + nextFileSize;
boolean needNewFile = predictedZipSize >= maxSize;
System.out.println("[=] ZIP current (" + formater.format(currentZipSize) + ") + next file (" + formater.format(nextFileSize) + ") = predicted (" + formater.format(predictedZipSize) + ") > max (" + formater.format(maxSize) + ") ? " + needNewFile);
if (needNewFile) {
safeClose(zip);
Path tmpFile = Files.createTempFile("teste-", (".part." + count++ + ".zip"));
System.out.println("[#] Starting new file: " + tmpFile);
zip = new ZipOutputStream(Files.newOutputStream(tmpFile));
zip.setLevel(Deflater.BEST_COMPRESSION);
response.add(tmpFile);
currentZipSize = 0;
}
ZipEntry zipEntry = new ZipEntry(currentFile.getName());
System.out.println("[<] Adding to ZIP: " + currentFile.getName());
zip.putNextEntry(zipEntry);
FileInputStream in = new FileInputStream(currentFile);
zip.write(in.readAllBytes());
zip.closeEntry();
safeClose(in);
long compressed = zipEntry.getCompressedSize();
System.out.println("[=] Compressed current file: " + formater.format(compressed));
currentZipSize += zipEntry.getCompressedSize();
}
safeClose(zip);
return response;
}
public static void safeClose(Closeable... closeables) {
if (closeables != null) {
for (Closeable closeable : closeables) {
if (closeable != null) {
try {
System.out.println("[X] Closing: (" + closeable.getClass() + ") - " + closeable);
closeable.close();
} catch (Throwable ex) {
System.err.println("[!] Error on close: " + closeable);
ex.printStackTrace();
}
}
}
}
}
}
そしてコンソール出力:
[?] Files to process: [\data\teste\TestFile(1).pdf, \data\teste\TestFile(2).pdf, \data\teste\TestFile(3).pdf, \data\teste\TestFile(4).pdf, \data\teste\TestFile(5).pdf, \data\teste\TestFile(6).pdf, \data\teste\TestFile(7).pdf]
[=] ZIP current (3.145.728) + next file (1.014.332) = predicted (4.160.060) > max (3.145.728) ? true
[#] Starting new file: C:\Users\Cassio\AppData\Local\Temp\teste-3319961516431535912.part.0.zip
[<] Adding to ZIP: TestFile(1).pdf
[X] Closing: (class java.io.FileInputStream) - java.io.FileInputStream@3d99d22e
[=] Compressed current file: 940.422
[=] ZIP current (940.422) + next file (1.511.862) = predicted (2.452.284) > max (3.145.728) ? false
[<] Adding to ZIP: TestFile(2).pdf
[X] Closing: (class java.io.FileInputStream) - java.io.FileInputStream@49fc609f
[=] Compressed current file: 1.475.178
[=] ZIP current (2.415.600) + next file (2.439.287) = predicted (4.854.887) > max (3.145.728) ? true
[X] Closing: (class java.util.zip.ZipOutputStream) - java.util.zip.ZipOutputStream@cd2dae5
[#] Starting new file: C:\Users\Cassio\AppData\Local\Temp\teste-8849887746791381380.part.1.zip
[<] Adding to ZIP: TestFile(3).pdf
[X] Closing: (class java.io.FileInputStream) - java.io.FileInputStream@4973813a
[=] Compressed current file: 2.374.718
[=] ZIP current (2.374.718) + next file (2.385.447) = predicted (4.760.165) > max (3.145.728) ? true
[X] Closing: (class java.util.zip.ZipOutputStream) - java.util.zip.ZipOutputStream@6321e813
[#] Starting new file: C:\Users\Cassio\AppData\Local\Temp\teste-6305809161676875106.part.2.zip
[<] Adding to ZIP: TestFile(4).pdf
[X] Closing: (class java.io.FileInputStream) - java.io.FileInputStream@79be0360
[=] Compressed current file: 2.202.203
[=] ZIP current (2.202.203) + next file (292.918) = predicted (2.495.121) > max (3.145.728) ? false
[<] Adding to ZIP: TestFile(5).pdf
[X] Closing: (class java.io.FileInputStream) - java.io.FileInputStream@22a67b4
[=] Compressed current file: 230.491
[=] ZIP current (2.432.694) + next file (4.197.512) = predicted (6.630.206) > max (3.145.728) ? true
[X] Closing: (class java.util.zip.ZipOutputStream) - java.util.zip.ZipOutputStream@57855c9a
[#] Starting new file: C:\Users\Cassio\AppData\Local\Temp\teste-17160527941340008316.part.3.zip
[<] Adding to ZIP: TestFile(6).pdf
[X] Closing: (class java.io.FileInputStream) - java.io.FileInputStream@3b084709
[=] Compressed current file: 3.020.115
[=] ZIP current (3.020.115) + next file (1.556.237) = predicted (4.576.352) > max (3.145.728) ? true
[X] Closing: (class java.util.zip.ZipOutputStream) - java.util.zip.ZipOutputStream@3224f60b
[#] Starting new file: C:\Users\Cassio\AppData\Local\Temp\teste-14050058835776413808.part.4.zip
[<] Adding to ZIP: TestFile(7).pdf
[X] Closing: (class java.io.FileInputStream) - java.io.FileInputStream@63e2203c
[=] Compressed current file: 1.460.566
[X] Closing: (class java.util.zip.ZipOutputStream) - java.util.zip.ZipOutputStream@1efed156
[>] Generated ZIP files(s): [C:\Users\Cassio\AppData\Local\Temp\teste-3319961516431535912.part.0.zip, C:\Users\Cassio\AppData\Local\Temp\teste-8849887746791381380.part.1.zip, C:\Users\Cassio\AppData\Local\Temp\teste-6305809161676875106.part.2.zip, C:\Users\Cassio\AppData\Local\Temp\teste-17160527941340008316.part.3.zip, C:\Users\Cassio\AppData\Local\Temp\teste-14050058835776413808.part.4.zip]