How to append to zip files

I would like to know how to append to a zip file, I know how to write a zip file and I can put as many entries in the first time but if I run my program again and try to add a file to the same zip file it erases what was already in it. I use ZipOutputStream(new FileOutputStream), I have tried the FileOutputStream append but it does not seem to work for zip files, I have tried RandomAccessFile on my zip file but that just seems to add the bytes to the file that already in the zip file, and makes the file unopenable, corrupt. If you can help, please tell me how.

You actually have to open the previous zipped file, read it's entries and their data and then rewrite a new zip file with these entries and their data. You can then delete your first file and rename the new one so the effect is that you have "appended" zip files to the existing archive.
Here is an example which I pieced together from a larger bit of code that I have. This would need to be in a try/catch but this should give you a start.
/** This method adds a new file to an existing zip file. It * includes all the zipped entries previously written.
* @param file The old zip archive
* @param filename The name of the file to be Added
* @param data The data to go into the zipped file
* NOTE: This method will write a new file and data to an archive, to
* write an existing file, we must first read the data frm the file,
* then you could call this method.
public void addToArchive(File file, String filename, String data)
     ZipOutputStream zipOutput = null;
ZipFile zipFile = null;
Enumeration zippedFiles = null;
ZipEntry currEntry = null;
ZipEntry entry = null;
zipFile = new ZipFile( file.getAbsolutePath() );
     //get an enumeration of all existing entries
zippedFiles = zipFile.entries();
     //create your output zip file
zipOutput = new ZipOutputStream( new FileOutputStream ( new File( "NEW" + file.getAbsolutePath() ) ) );
//Get all the data out of the previously zipped files and write it to a new ZipEntry to go into a new file archive
while (zippedFiles.hasMoreElements())
//Retrieve entry of existing files
currEntry = (ZipEntry)zippedFiles.nextElement();
//Read data from existing file
BufferedReader reader = new BufferedReader( new InputStreamReader( zipFile.getInputStream( currEntry ) ) );
String currentLine = null;
StringBuffer buffer = new StringBuffer();
while( (currentLine = reader.readLine() ) != null )
buffer.append( currentLine);
//Commit the data
zipOutput.putNextEntry(new ZipEntry(currEntry.getName()) ) ;
zipOutput.write (buffer.toString().getBytes() );
zipOutput.flush();
zipOutput.closeEntry();
//Close the old zip file
zipFile.close();
//Write the 'new' file to the archive, commit, and close
entry = new ZipEntry( newFileEntryName );
zipOutput.putNextEntry( entry );
zipOutput.write( data.getBytes() );
zipOutput.flush();
zipOutput.closeEntry();
zipOutput.close();
     //delete the old file and rename the new one
File toBeDeleted = new File ( file.getAbsolutePath() );
toBeDeleted.delete();
     File toBeRenamed = new File ( "NEW" + file.getAbsolutePath() );
toBeRenamed.rename( file );
Like I said I haven't tested this code as it is here but hopefully it can help. Good luck.

Similar Messages

  • Append to Zip file

    Hi everyone,
    I have created a zip file (test.zip), but i cant figure out how to append a text file to it.
    Can anybody help?

    http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/ZipInputStream.html#createZipEntry(java.lang.String)

  • How to insert a ZIP file in the message body and display it as icons

    how to attach an ZIP file as an inline attachment. whether its possible in java mail.

    Stop asking the same thing over and over.
    Keep the discussion in your original thread.

  • How to open a zip-file in Bridge for PC?

    How to open a zip-file in Bridge for PC?

    I think it's built in, just double click on it and it should open with Archive Utility. I dunno, maybe Unrar X might work for you>
    JB

  • How to store the zip file in oracle table?

    hi,
    How to store the zip file in oracle table ?
    is it possible to unzip and read the file ?
    Thanks
    Rangan S

    SQL> DESC BLOB_TABLE;
    Name Type Nullable Default Comments
    A INTEGER Y
    B BLOB Y
    SQL> INSERT INTO BLOB_TABLE VALUES(5,BLOB('MWDIR_TST','TEST.ZIP'));
    INSERT INTO BLOB_TABLE VALUES(5,BLOB('MWDIR_TST','TEST.ZIP'))
    ORA-00904: "BLOB": invalid identifier
    SQL> INSERT INTO BLOB_TABLE VALUES(5,('MWDIR_TST','TEST.ZIP'));
    INSERT INTO BLOB_TABLE VALUES(5,('MWDIR_TST','TEST.ZIP'))
    ORA-00907: missing right parenthesis
    SQL> INSERT INTO BLOB_TABLE VALUES(5,('\\MWDIR_TST\TEST.ZIP'));
    INSERT INTO BLOB_TABLE VALUES(5,('\\MWDIR_TST\TEST.ZIP'))
    ORA-01465: invalid hex number
    SQL> INSERT INTO BLOB_TABLE VALUES(5,('\\MWDIR_TST\TEST.ZIP'));

  • How to email a .zip file attachment from PC

    Hi,
    Please, let me know how to email a .zip file attachment from Presentation Server.
    Thanks,
    Madhuri.

    Hi,
    try fm SO_DOCUMENT_SEND_API1
    it's well documented (look with SE37)
    and look here:
    /people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface
    regards Andreas

  • How to check & unzip zip file using java

    Dear friends
    How to check & unzip zip file using java, I have some files which are pkzip or some other zip I want to find out the type of ZIp & then I want to unzip these files, pls guide me
    thanks

    How to check & unzip zip file using java, I have
    ve some files which are pkzip or some other zip I
    want to find out the type of ZIp & then I want to
    unzip these files, pls guide meWhat do you mean "other zip"? Either they're zip archives or not, there are no different types.

  • How do you use zip files?

    Hello all,
    I have always stayed away from ZIP files (as in doing it myself, to my own files), since my days in Windows, after all the countless corrupt ZIP files I received from others, that simply would not UNzip...
    But recently I've come across references to them again, and I realised I might be missing a trick or two....
    Any comments on the following would be appreciated:
    1.) What can I use them for?
    As in - could I ZIP my entire folder of holiday pictures - all 13GB's of them?
    Or is there some sort of size limit, where anything over, and things become problematic?
    Are some file types Zipped better than others?
    2.) How does it actually work?
    As in - what happens to my original files?
    Assume the holiday pics are Zipped - do the orginals get compressed, or can I select it so that copies are Zipped, as a kind of backup?
    3.) How safe is it?
    As in - assuming my hardware and software are all OK, nothing buggy etc. - if I create the ZIP file, pop it somewhere on a USB/External/the same laptop, and then Unzip it later, when I need them again - am I correct in assuming that the chances of the file being corrupted, are relatively slim?
    And coming back to those holiday pics - could the compression process result in a loss in picture quality/resolution, were I to then unzip the file? Or - assuming everything works as planned - will the quality of the pictures/file emerge exactly the same as they were prior to being compressed?
    I realise all of the above are ridiculously simple questions - but googling this doesn't really help, as most sites simply explain the how to, not the WHY...
    The WHY articles were probably all written 15 years ago already... I'm just very, very far behind....
    Thanks in advance!
    [Mid-2012 MBP 8GB Ram; OSX ML]

    Zipping an image file should have no effect on image quality, as the image data is not actually changed.
    Note that there's very little point in zipping a folder full of images, unless the images are not a compressed file type. You can't compress data that is already compressed - in fact, trying to do so often increases the file size - and JPEG images, for example, are already compressed.
    For the most part, you don't need to use zip at all. I absolutely DO NOT recommend storing your files and/or folders on the hard drive in compressed form as a space-saving measure. It doesn't save enough space to be worth the time needed to compress files large enough to warrant it on a modern drive. Accessing zipped files is a pain, too, since you basically have to decompress the whole thing in order to access a file inside a zip file.
    Zip becomes useful if you need to attach a large number of files to an e-mail message (zip the files, then attach the single zip file) or want to create an archive of multiple files to download from a web site through a single link. In other words, in situations where you need to transmit many files over the internet, especially where Windows users may be involved, zipping files can be either important or a convenience.

  • How to unzip a zip file within another zip file

    I've got code that successfully processes a file within a zip file.
    But now the zip file can also contain other zip files. How can I
    process a zip nested within a zip without actually extracting the
    files? It looks like I need a ZipFile object to be able to take
    advantage of the zip classes for reading zip entries. But the
    ZipFile methods want as an argument an actual file, as opposed
    to some object in memory. I can read the 'inner' zip file into a
    ZipInputStream object, but then how do I make that available for
    processing as a ZipFile? I've searched all over and cannot find
    anything anywhere that talks about working with nested zip files.
    Does anyone have any sample code that does this? Thanks!

    I have successfully done it.
    You cannot have the code - it is proprietary.
    However here are a few pointers:
    o Use ZipStreams rather than ZipFile.
    o ZipStream is just a filter on a stream, so you can have a zipstream open, read a ZipEntry, then ask for a substream for the stuff up to the next ZipEntry. Put a second ZipStream on that substream.
    o As I recall you have to watch out that your inner ZipStream does not do a close. (Everything will get closed.) Instead, there is another ZipStream method that will close without closing all the underlying streams.
    Hope this helps.

  • How to import a zipped file?

    Hi-
    I am trying to import a zipped file (UCSF hospitalist handbook) into itunes, so I can then put it on my iphone. I've used this handbook on my palm pilot for years, but would like to now use it on my iphone. I cannot seem to import it into itunes though. I downloaded from here: http://www.meistermed.com/isilodepot/isilodocs/isilodocs_ucsf_hospitalisthandbook.htm:
    but then don't know how to get into itunes. Any help is greatly appreciated!

    Zip files are like wrapped presents. You have to unzip them to see what goodies are inside!
    Usually double-clicking the Zip file will give you a dialog to start the extraction.
    If by any chance that doesn't work, right-clck the ZIP file and choose Open With > Compressed Folders.
    Presumably the contents will turn out to be iTunes-compatible files and you can add them to your library.

  • How to unzip and zip files

    I have a very large number of Karaoke music that plays on my windows pc but some will not play on my Mac. I think i need to unzip & re-zip them to play on my mac but i'm not sure exactly how to do this. any suggestions? Do I need a special program? running os x 10.9.3

    Zip files can be opened with the built in 'Archive Utility' application. Double click any zip file & it will try to unzip it.
    This application is tucked away inside /System/Library/CoreServices/Archive Utility.  If you want to set preferences for it you need to open the location in Finder by using the 'Go Menu > Go to Folder…', enter /System/Library/CoreServices/ for the path & then open the Archive Utility application and look in it's preferences.
    I expect that zipping & rezipping will not do anything useful for you. I suspect that the file format is the issue.
    Unzip some files & post what the Finders 'File > Get info' panel shows, take a screenshot of it with 'cmd+shift+4', then drag the crosshairs to select the area. it will appear on the Desktop for uploading here.
    You may need to find a compatible player or find a way to convert them if the Mac cannot play them.
    P.S. to zip a file(s) select the items & right click, select 'Compress x items' & you will get a zip archive - I don't think it will help you though

  • How to create a zip file whith passwprd ?

    Hi Im trying to create a zip file using the java zip support, the zip file is being created correctly now I have one doubt I would like to know if the zip file can be protected with a password as Winzip does, if so could you tell me please how to do it.
    Regards,
    Antonio
    [email protected]

    With Core Java, for all I know you still can't.

  • How to Load OE Zip Files from C drive to Linux VM?

    I am running Windows 7 Professional, 8GB RAM, quad-core, 1.5 GHz, 560 GB hard drive.
    I downloaded Linux into Virtual Box and it runs correctly.
    I downloaded Oracle Database Enterprise (OE) 11gR2 onto my C drive (two zip files, 32-bit).
    Now how do I install OE on Linux? (Can't figure out how to get it from my C dirve to the VM.)
    Please be as detailed as your patience permits.
    Thanks!

    Download WinSCP program. And connect to linux. There is left side and right side. The right side is linux side. And the left is windows side. Choose zip files from left side and press F5 key to copy to linux side.
    http://winscp.net/eng/download.php
    Talip Hakan Ozturk
    http://taliphakanozturken.wordpress.com/

  • How to append two CSV files using ftp

    Hi
    Please let me know the FTP command to append the two .CSV files into one .CSV file.
    e.g.
    Let me explain:
    one CSV file has the fields F1 , F2, F3 , F4 and has 5 records
    another CSV file has the same sequence of fields F1, F2, F3, F4 and has 10 records
    after appending both the files , I must get 15 records.
    Thanks
    Alok

    Ok I will try out.
    I am illustrating my requirement as follow
    File#1 (type .CSV)
    F1     F2     F3     F4
    100   566    89     86   
    235   256    56     12
    File#2 (type .CSV)
    F1     F2     F3     F4
    56     56     98     102
    12     23     36      523
    23      56     56    89
    Now we want to Append File#1 to File#2 as follow: 
    F1    F2    F3     F4
    100   566    89     86   
    235   256    56     12   
    56     56     98     102
    12     23     36      523
    23      56     56    89
    Please suggest now that which command would be appropiate.
    Alok

  • How do you install zipped files

    I have downloaded a zipped file and when I open it to install it I can't find where it goes. I have ended up saving it about ten times. I am new with the apple but this is the first time I have not been able to install a download. Thanks for your help.

    If you have downloaded the zipped file from the web, it should go into your "Downloads" folder, based on the preferences in your web browers.
    Are you using Safari? If you are, go to the downloads window, right-click, select "Show in Finder".
    Once the file is unzipped, you can select the unzipped disk image or installer and launch it to install your application.

Maybe you are looking for

  • Outlook Integration: unable to create snapshot templates.

    Dear all, I followed all the steps mentioned in note 861055 "You can easily repeat the step of template creation after running the server installation:    1. Start the Addon Outlook Integration from Business one.    2. Choose/Administration/Outlook I

  • Vendor Interest calculation with penalty calculation

    Dear All, We have some requiremnt from my client. We receive Deposit from our Loan Creditor for a specified period and refund/payback the amount along with interest after completion of the tenure of the deposit. Meanwhile,if the depositor wants to wi

  • 3M cloud app.

    Frequently books on hold become hidden, not visible to me.  Have to frequently delete and reinstall 3m cloud app.  Now getting message - device activation failed.  Too many activations for Adobe ID.  Please help, in the middle for readings books.  Th

  • How do I access files on my Macbook from my iMac?

    I'm about four months new to my Macbook and I'd like to access just files on my Macbook from my husband's iMac. We have wi-fi in the house. Thanks!

  • Error connecting to Yahoo Messenger - internal error 100

    I have this problem since past 2 days. My YM was connected for few seconds and the disconnected. The message appear was internal error 100. I am using the latest YM application. Please help me how do i fix this? If im using Google talk, there is no p