Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to write an algorithm in java (for an android app) to read a folder containing more folders and each of those containing images and audio files so the structure is this: mainDir/subfolders/myFile1.jpg

  • It must be in java, something like perl script is not an option. It would preferably be for the compressed archive in order to squeeze as many files as possible before mailing the zip. Just a normal zip (no jar).

My problem is that I need to limit the size of the archive to 16mb and at runtime, create as many archives as needed to contain all my files from my main mainDir folder. I tried several examples from the net, I read the java documentation, but I can't manage to understand and put it all together the way I need it.

Has someone done this before or has a link or an example for me? I resolved the reading of the files with a recursive method but I can't figure the logic for the zip creation.

EDIT: FileNotFoundException (no such file or directory) this was my initial post at Stack Overflow. I've got an answer to it, but I can't set the size of the ZipEntry and the logic doesn't work and also when extracting the my files from the zip I get the compression method not supported error.

share|improve this question
Does this have to be in Java? Would a (for example) perl script to create the archives be acceptable? Is the limit on the size of the compressed or uncompressed archive? Is this a .jar (with manifests and the like) or just a simple .zip? – user40980 Jun 19 '12 at 16:46
It must be in java yes. It would preferably be for the compressed archive in order to squeeze as many files as possible before mailing the zip. Just a normal zip (no jar). – Marcel Căşvan Jun 19 '12 at 19:42
up vote 4 down vote accepted

The size limit (16 mb or whatever) does not enforce you to have archive size as close to it as possible.

Assuming that you are allowed to create archives of smaller size, here is the "first iteration" solution - dead simple, but meets your requirements: just zip every file into separate archive.

  1. myFile1 ->
  2. myFile2 ->
  3. etc

Now, if you want it a bit less dumb, use the sum of current archive size (Deflater.getBytesWritten()) and next uncompressed file size to decide if it's time to switch to creating new archive.

  1. myFile1 ->
  2. size of plus myFile2 within limit -> add myFile2 to archive1
  3. size of plus myFile3 exceeds limit -> add myFile3 to new zip, archive2
    Yeah there is a chance that adding compressed myFile3 to archive1 will remain within limit, but why bother?
  4. etc
share|improve this answer
Good point! I will try to implement your logic tomorrow. Thank you – Marcel Căşvan Jun 19 '12 at 19:46
its funny how some people abuse their power... way to go discouraging beginners – Marcel Căşvan Jun 19 '12 at 20:31
@maple_shaft I too thought it was a poor question, until I read another one mentioned at prior SO post from the OP and discovered that straightforward approach is really tough "Took about a day to write and 2 days to debug. specification - section K has the info you are looking for specifically, but you'll need to read A, B, C and F as well..." That made me think, what the hell section K and A, B, C and WTF as well... question is not that easy as it looks – gnat Jun 19 '12 at 21:15
@MarciCăşvan some people abuse their power If that's a comment on maple_shaft closing the question, I fully support his decision. I only re-opened it after gnat edited it into shape, and that's how it's supposed to work. Moderators are not here to fix every question, that's what the community is for, the process was followed perfectly for your question and I strongly believe that there was no abuse of power here. – Yannis Jun 21 '12 at 3:39
thanks for the response I successfully managed ti implement the logic – Marcel Căşvan Jun 21 '12 at 7:24

Are you using Java 7? If so then it has native support for Zip archives in the new NIO.2 APIs

The zip file system provider introduced in the Java SE 7 release is an implementation of a custom file system provider. The zip file system provider treats a zip or JAR file as a file system and provides the ability to manipulate the contents of the file. The zip file system provider creates multiple file systems — one file system for each zip or JAR file.

The demo/nio/zipfs/ file in your Java SE 7 installation contains the source code for the zip file system provider. It also contains the class that shows how to use the zip file system provider...

You can use the factory methods of the java.nio.file.FileSystems class to create a new zip file system or to obtain a reference to an existing zip file system...

share|improve this answer
I need to check out the version of java. Thanks, I will look up the new NIO.2 API's tomorrow and get back with an answer. – Marcel Căşvan Jun 19 '12 at 19:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.