通常のファイルを .zip に圧縮し、新しいファイルを作成せずに gzip ファイルとして圧縮したい。
たとえば、pdf ドキュメントdoc.pdfがあるとします。取得する必要があるのは次のとおりです。doc.pdf.zip.gz
doc.pdf.zip
という名前の新しいファイルを作成してから開き、gzip したくありません。
サーバーを使用してブラウザからファイルを取得し、それを返します。これが私の機能です。
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
あり、圧縮されていません。なぜですか??