I want to compress a normal file to a .zip and then compress it as gzip file without creating a new file.
For example let's say that I have a pdf document doc.pdf, what I have to get is: doc.pdf.zip.gz
I don't want to creat a new file called doc.pdf.zip
and then open it and gzip it.
I'm working with a server to get the file from the browser and return it back, this is my function:
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();
}
}
I get a file doc.pdf.zip.gz
but it has a corrupted file doc.pdf
and not zipped why??
I think you can create a temporary file for the zip add it to gzip and then delete the zip that was created. The below code sample should help you with that.
Hope it helps.