Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid append files to a zip file without having to re-write the entire zip file?
    primarykey
    data
    text
    <p>How can I append files to an existing zip file? I already have the code that can create a zip file and it works great except for one big problem. The way it works now, the user takes a bunch of pictures, and at the end, all the pictures get added to a zip file, which can take quite a while if you take enough pictures. :-( So I'm thinking, I have a very good and efficient solution. As the pictures are taken, I will simply add the each new picture to the zip file right after it's taken. Then when they're done taking pictures, finish up the zip file so it's usable and export it. :-) </p> <p>The problem is, I can not get it to add files to an existing zip file. :-( Here's what I have so far. Also, please keep in mind, this is just a proof of concept, I do understand that re-initializing everything for every iteration of the for loop is very dumb. Each iteration of the loop is supposed to represent another file being added which will most likely be a long time later, maybe even an hour later, which is why I have everything resetting each iteration, because the app will be shut down between adding files. If I can get this working, then I will actually ditch the for loop and put this code into a function that gets called every time a picture gets taken. :-)</p> <pre><code> try { for(int i=0; i &lt; _files.size(); i++) { //beginning of initial setup stuff BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(_zipFile,false); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[BUFFER]; out.setLevel(0); //I added this because it makes it not compress the data //at all and I hoped that it would allow the zip to be appended to //end of initial setup stuff //beginning of old for loop Log.v("Compress", "Adding: " + _files[i]); FileInputStream fi = new FileInputStream(_files[i]); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); //end of for old loop //beginning of finishing stuff out.close(); //end of finishing stuff } } catch(Exception e) { Log.e("ZipCreation", "Error writing zip", e); e.printStackTrace(); } </code></pre> <p>Also, I have experimented around with </p> <pre><code>FileOutputStream dest = new FileOutputStream(_zipFile,true); </code></pre> <p>If you notice, I set append to true, which will actually append the data to an existing file. And what's interesting is, it actually does append the data to the original file, however, after the file gets extracted on my computer, the last file written is all that gets extracted, which is bad. :-( So is there some way to start writing a zip file, and then later, add on to it, and finish up the zip file? I've even thought about possibly taking ZipOutputStream and modifying it to fit this model that I need. It should logically be possible somehow? :-)</p> <p>Thanks in advance for the help! :-D</p> <p>-Jared</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload