I am having a requirement wherein i have to create a zip file from a list of available files. The files are of different types like txt,pdf,xml etc.I am using java util classes to do it.
The requirement here is to maintain a maximum file size of 5 mb. I should select the files from list based on timestamp, add the files to zip until the zip file size reaches 5 mb. I should skip the remaining files.
Please let me know if there is a way in java where in i can estimate the zip file size in advance without creating actual file?
Or is there any other approach to handle this
If you type unzip -l <zipfile> , it prints a listing of files within the zip, with their uncompressed sizes, then the total uncompressed size of all of them. This is human-readable output, but you can get a machine-readable number using unzip -l <zipfile> | tail -n1 | awk '{ print $1 }' . Save this answer.
Java get file size using File classJava File length() method returns the file size in bytes. The return value is unspecified if this file denotes a directory.
You can compress, or zip, the file in Windows, which shrinks the size of the file but retains the original quality of your presentation. You can also compress the media files within the presentation so they're a smaller file size and easier to send.
Always empty zip files will be 22 bytes (or say less than 100bytes) ? How to categorize empty & corrupt zip files ?
Wrap your ZipOutputStream into a personalized OutputStream, named here YourOutputStream.
ZipOutputStream
(zos2) which wraps a new ByteArrayOutputStream
(baos)public YourOutputStream(ZipOutputStream zos, int maxSizeInBytes)
YourOutputStream
, it will first write it on zos2public void writeFile(File file) throws ZipFileFullException
public void writeFile(String path) throws ZipFileFullException
baos.size()
is under maxSizeInBytes
You need two ZipOutputStream, one to be written on your drive, one to check if your contents is over 5MB.
EDIT : In fact I checked, you can't remove a ZipEntry easily.
http://download.oracle.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html#size()
+1 for Colin Herbert: Add files one by one, either back up the previous step or removing the last file if the archive is to big. I just want to add some details:
Prediction is way too unreliable. E.g. a PDF can contain uncompressed text, and compress down to 30% of the original, or it contains already-compressed text and images, compressing to 80%. You would need to inspect the entire PDF for compressibility, basically having to compress them.
You could try a statistical prediction, but that could reduce the number of failed attempts, but you would still have to implement above recommendation. Go with the simpler implementation first, and see if it's enough.
Alternatively, compress files individually, then pick the files that won't exceedd 5 MB if bound together. If unpacking is automated, too, you could bind the zip files into a single uncompressed zip file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With