minmaxavg minmaxavg - 5 months ago 10
Java Question

Adding files to zip java using memory while avoiding reserved file name problems

I want to add, remove or modify files in zip using the most effective way possible.

Yes, you may say what I should do is to unzip/zip files into file system, but if there is a file with special name like 'aux' or 'con' , It doesn't work in Windows as they are DOS device names, and also there might be filename encoding issues that prevents the process from working proberly. Another reason I don't just unzip to file system and re-zip is that it is much more slower and takes more disk space than just using RAM.

In image : http://i.stack.imgur.com/yPuYG.png

Answer

You could use a memory bases stream, like ByteArrayOutputStream to read/write the contents of the file.

The issue is the amount of available memory, because RAM is limited, you're going to need to store the output on something larger, like a disk eventually.

In order to try and optimism the process, you could set a preferred threshold for the read/write/process operation.

Basically you would run the process and calculate how long it took, based on the preferred threshold, adjust the buffer size for the next loop.

I would allow for a number of loops and average the time so your not trying to do fine control over the buffer that might actually slow you down

Comments