my data is stored deflated in a database. You can donwload this data inflated as .txt via app. Now it should be possible to download the data as .zip
I'm looking for a solution without decompressing the data and compressing it again via
// byte[] deflated = ...;
// File zipFile = ...;
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos))
{
ZipEntry entry = new ZipEntry("data.txt");
zos.putNextEntry(entry);
zos.write(inflate(deflated));
} catch (Exception e) {
// handling
}
I tried to work with entry.setMethod(ZipEntry.STORED)
but it didn't work.
Google search only delivers solutions with uncompressed data.
Any idea?
GZip or 7z may be possible too.
.setMethod(STORED)
is like calling zip -0
: It's telling the Zip library that you want the data to be stored uncompressed. It's not what you want.
The zip library is not capable of delivering on this.
There's a somewhat more expansive zip library out there that supports more of zip's features: lingala-zip4j.
I don't think that library can do the job either. So, if you want to do this, you'd have to fork it. At least you now have a thing to fork.
Assuming the data you have is compressed with DEFLATE, you can indeed do what you want - it's just a matter of writing the right zip header info. Forking lingala, or handwriting your own, given that the only thing you need it to do is emit zip files that most zip tools can read, is not actually all that complicated; the zip header structure is a bit weird but you just need enough to successfully compress your own data. Wikipedia for some reason has the full breakdown of the zip format if you prefer to just write it from scratch instead of forking lingala.