zip 形式の DOCX ファイルでマイナーなテキスト置換を行うためのツールを作成しています。私の方法はZipEntry
、元のファイルのエントリから変更されたファイルにZipOutputStream
. ほとんどの DOCX ファイルではこれでうまくいきますが、ときどき、私が書いた内容とファイルに含まれるメタ情報(通常は圧縮サイズの違い)ZipException
の間の不一致に関する問題に遭遇することがあります。ZipEntry
コンテンツをコピーするために使用しているコードは次のとおりです。簡潔にするために、エラー処理とドキュメント処理を省略しました。今のところ書類記入で困ったことはありません。
ZipFile original = new ZipFile(INPUT_FILENAME);
ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(OUTPUT_FILE));
Enumeration entries = original.entries();
byte[] buffer = new byte[512];
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();
if ("word/document.xml".equalsIgnoreCase(entry.getName())) {
//perform special processing
}
else{
outputStream.putNextEntry(entry);
InputStream in = original.getInputStream(entry);
while (0 < in.available()){
int read = in.read(buffer);
outputStream.write(buffer,0,read);
}
in.close();
}
outputStream.closeEntry();
}
outputStream.close();
ZipEntry
オブジェクトをあるオブジェクトからZipFile
別のオブジェクトに直接コピーする適切な方法または慣用的な方法は何ですか?