通常のファイルを.zipに圧縮し、新しいファイルを作成せずにgzipファイルとして圧縮したいとします。 doc.pdf.zip.gz
が.zipをjavaの.gzに圧縮する方法は?
私はdoc.pdf.zip
と呼ばれる新しいファイルをレコード生成し、それとgzipを開くにはしたくない:
例えば者は、doc.pdf、私が取得する必要がありますが私はPDF文書を持っているとしましょうそれ。
私は、ブラウザからファイルを取得し、戻ってそれを返すようにサーバに働いている、これは私の関数である:
public void ZIPandGZIP(String fileName, OutputStream os, String header) throws FileNotFoundException
{
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
DataOutputStream dos = new DataOutputStream(os);
try {
dos.writeBytes(header);
ZipOutputStream zpos = new ZipOutputStream(os);
zpos.putNextEntry(new ZipEntry(fileName));
GZIPOutputStream gos = (new GZIPOutputStream(zpos));
fis.read(data);
gos.write(data);
zpos.flush();
zpos.close();
gos.flush();
gos.close();
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
私はファイルdoc.pdf.zip.gz
を得るが、それは破損したファイルdoc.pdf
とzip形式ではないがありますなぜ??
'close()'呼び出しを 'finally'ブロックに移動します。 –
ありがとう、私はそれをしましたが、同じことを返し続けます。 –
はあなたの問題とは関係なく、最良の方法です:P –