ZipOutputStream appends characters to beginning of zipped file?

Hi and thanks in advance for any help!
I am trying to zip Excel files using ZipOutputStream class. I can open the Excel files fine before I zip. Then I run my program to zip. It seems to work. I use WinZip to unzip and when I try and open unzipped file in Excel, Excel says it does not recognize format. Looking at unzipped file in text editor, about 15 wierd characters and nulls are now appended to the beginning of my Excel file that are not in the original. If I delete them in the text editor, Excel can open.
Here is my zip code:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ArrayList fileNames = new ArrayList();
fileNames.add ("91PSAG0Jak.csv");
fileNames.add ("91I9F60K62.csv");
// Compress the files
for (int i=0; i<2; i++) {
String filename = String.valueOf(fileNames.get(i));
byte[] file = DatabaseUtilities.downloadFile("pdfstoragedir", filename);
// Add ZIP entry to output stream.
zos.putNextEntry(new ZipEntry(filename));
ObjectOutputStream oos = new ObjectOutputStream (zos);
oos.writeObject(file);
oos.flush();
// Complete the entry
zos.closeEntry();
//Complete the ZIP file
zos.close();
byte[] zipFile = baos.toByteArray();
DatabaseUtilities.uploadFile("pdfstoragedir", "zipOutput.zip", zipFile);
FileOutputStream fos = new FileOutputStream("C:\\zipOutput.zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(zipFile, 0, zipFile.length);

Thanks, I got it I think, I can write my byte[] directly to the zipOutputStream:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ArrayList fileNames = new ArrayList();
fileNames.add ("8aCeOH0Fba.zip");
fileNames.add ("8da6S40H4e.zip");
// Compress the files
for (int i=0; i<2; i++) {
String filename = String.valueOf(fileNames.get(i));
byte[] file = DatabaseUtilities.downloadFile("pdfstoragedir", filename);
// Add ZIP entry to output stream.
zos.putNextEntry(new ZipEntry(filename));
zos.write(file);
// Complete the entry
zos.closeEntry();
//Complete the ZIP file
zos.close();
byte[] zipFile = baos.toByteArray();
DatabaseUtilities.uploadFile("pdfstoragedir", "zipOutput.zip", zipFile);
FileOutputStream fos = new FileOutputStream("C:\\zipOutput.zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(zipFile, 0, zipFile.length);

Similar Messages

  • How to append content in begining of text file?

    i want to append content in the begining of text file.
    (usually append flag will append content in end of the file)
    is there any class in java support this need?
    if there is no class for this...anybody can give logic to implement
    venu

    Duplicate post
    http://forum.java.sun.com/thread.jspa?threadID=577639

  • Append characters to an existin excel file

    Hello guys
    I've an excel file where I write an array (single data type)of 6 elements by using "Write to Spreadsheet File.vi" On this file I also need to write a new column with the time and date.At this point everthing it´s ok, but when I´m trying to append this new column(date & time) with "Write character to File.vi" it writes this data not in the rigth column but it does it in the first column of the file. Could it be wrong by problems with the EOF marker??
    I attached the excel file.
    Thanks in advance
    Attachments:
    test_RPila.xls ‏14 KB

    Hello,
    When I use the Write to Spreadsheet File.vi to append data to an existing Excel file, the data I input doesn't appear in the file.All I get is replace the Excel file by the new data and I loss the old data. This is due because Excel changes tab-delimited format. There is a KB than explains it:
    http://digital.ni.com/public.nsf/websearch/D1629D863F0442CC86256A0200558A15?OpenDocument
    You could use the Report Generation Toolkit to avoid this Excel formatting. Look at the following KB and you will see that it is possible:
    http://digital.ni.com/public.nsf/websearch/EFCE1C25DCE7483E86256CAF00539451?OpenDocument
    If you don't have Report Generation Toolkit, try to contact to NI
    regards
    crisR

  • 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.

  • Garbage characters in begining of Mac saved file

    I'm new to the Mac environment and Dreamweaver so please
    excuse me if this is a simple issue to solve. I've been having
    issues with Dreamweaver inserting garbage characters in the
    beginning of a file created and saved. In IE6 and 7, the pages do
    not format correctly due to these garbage characters. When I save a
    new file in Dreamweaver and copy it over to a PC, the garbage
    characters show up in any editor . Is there a setting in
    Dreamweaver to correct this issue?
    Below is the saved code..notice the garbage in front of the
    Ôªø@charset "UTF-8";
    /* CSS Document */
    body{ margin: 0; padding: 0;font-family: "Lucida Grande",
    Lucida, Verdana, sans-serif; height: 100%; font-size: 12px;
    background-image: url(../images/cmt_talentapp_bg.jpg);
    background-repeat: no-repeat;}
    a { text-decoration: none; color: #109cd9; }
    a.hover { text-decoration: underline; }
    #pageContainer { width: 945px; height: auto; border: 0px
    solid green; float: left; padding-left: 5px; padding-top: 0px;
    margin-top: 0px;}
    #headerContainer { padding: 0 10px 0 10px; width: 920px;
    height: 100px; border: 1px red solid; float: left; }
    #contentArea { width: 710px; padding: 20px 0 30px 0; border:
    1px red solid; float: left; height: auto; }

    It's just a shoot in the dark. Deending on your settings, DW
    may insert a
    BOM:
    http://en.wikipedia.org/wiki/Byte_Order_Mark
    At the beginning of UTF-8 encoded files.
    You may check your settings and try to change them to suit
    your needs, then
    see if the problem goes away.
    Massimo Foti, web-programmer for hire
    Tools for ColdFusion and Dreamweaver developers:
    http://www.massimocorner.com

  • Jar (or Zip) file with problems in accents, special characters

    Hi! I've a servlet that creates a jar (or zip) file and then send it. My problem is that when I create an entry in which the filename has special characters such as accents, when I unjar or unzip de file, it brings a lot of garbage characters. For example, the nex entry:
    Ex�menes.doc
    when unzipped is:
    Ex?�menes.doc
    I've tried a lot of things:
    -Setting the locale to ES, MX
    -Replacing all the letters with special characters with its unicode (like s.replace('�','\u00E1')
    -Trying to convert it to UTF8 (new String(path.getBytes(),"UTF8") )
    -Replacing the file separator char (according to a workaround that I found in the bug database)
    But nothing of this worked, alone or together. I've read that this is (or was) a bug in the API, but don't know if a solution has been found.
    Any help will be greatly appreciated!

    It's not clear what you are asking. Maybe this will help
    http://www.cfdev.com/code_samples/code.cfm/CodeID/83/Java/Simple_Ant_build_xml_Build_Task

  • I have really slow internet, i bought OS X mountain lion & tried for days to download it. my internet cuts every time & the download restarts from the beginning is thr a way to download the software as a "zip" file or from a link not app store?

    I have really slow internet, i bought OS X mountain lion & tried for days to download it. my internet cuts every time & the download restarts from the beginning is thr a way to download the software as a "zip" file or from a link not app store?

    MacBook Pro
    https://discussions.apple.com/community/notebooks/macbook_pro 
    https://discussions.apple.com/community/mac_os/mac_os_x_v10.7_lion?view=discussi ons
    http://www.apple.com/support/macbookpro
     Take it somewhere that has better broadband. The best you can do sometimes is invest in new modem that works with your system instead of the rental etc in most cases.
    Do a Safe Boot.
    Make sure your firewall is set properly. Don't use 3rd party. AV software can get in the way.
    Make sure you are getting the service and a clean strong modem signal - slow is one thing but if it drops off and can't maintain a connection you have hardware issues, not software - and upgrade to a new OS doesn't tend to improve, it may even not work with older networking equipment once you do get it.
     I agree and think electronic only goes too far. And when you do get it: burn the package and manually set it aside first before running the intaller! make a flash memory installer, put it on DVDs too.

  • Appending files to zip files

    Hi,
    My requirement is i have to append data to zip files using file receiver adapter.
    am also using FCC.
    can anybody help me on this how to approach.
    -Kishore
    Edited by: Kishore_Kumar_XI on Mar 3, 2012 6:05 PM

    Hi,
    I think you can use my blog case for your scenario
    ABAP PROXY TO FILE – Handling Heavy Volumes in SAP PI (ABAP PROXY TO FILE u2013 Handling Heavy Volumes in SAP PI)
    Steps:
    1. split into two scenario
         proxy --> XI --> file    (using above blog concept)
         (ii) file (zipped) --> Xi --> FTP (file) without IR objects
    using the first scenario write the file to XI server internal file location using NFS protocol using file receiver channel in append mode.
    Use the scripts as mentioned in the above blog (Note:- scripts will differ depending on XI Operating System). In the second script i.e.,  "Run OS Command After Message Processing" script, add the extra functionality which can zip and rename the file when it is last transaction. The last transaction can be identified from ABAP proxy message with little extra functionality in the report and using dynamic configuration we can pass this as input to second script which does the zipping and renaming.
    We are renaming the file for the last transaction, so that it can be pooled by second scenario file sender channel which will pool with the same naming convention. Pass this zip file directly to FTP server without using any IR objects.
    Regards,
    Praveen Gujjeti

  • Append a line at the beginning of a file via UTL_FILE

    Hello,
    is there a way to append a line to the beginning of a file instead of at the end of an existing file via UTL_FILE?
    Thanks in advance,
    Geert

    No, you would have to create a new file.

  • Appending "something" at the beginning of a file

    Hello,
    I am trying to append a character to the beginning of a file, doesnt seem to work with RandomFileAccess. I first called the seek() methond
    seek(0); //zero to send the file pointer to the front 
    write((byte) myCharValue);apparently it overwrites the value at the front instead of adding for example:
    before running the code the file has: "Java is Cool!"
    after running the code the file has: "xava is Cool!"
    Any Ideas?
    Thanks

    1. Create a new file.
    2. Write out whatever you wanted to insert at the beginning.
    3. Copy the data from the old file and append it to the new file.
    4. Close the files.
    5. Delete the old file.
    6. Rename the new file.

  • Read zip files created by ZipOutputStream

    Hello :)
    I am wondering if it is possible to create a zipfile using ZipOutputStream, which can be read using ZipInputStream with out using the ZipFile work-around discussed in http://forum.java.sun.com/thread.jspa?forumID=256&threadID=492219. This is no option for me, because we have a lot of client applications deployed, which cannot easily be altered.
    Are there any (third-party) libraries which I can use?
    -M1chael

    try it
    // These are the files to include in the ZIP file
    String[] source = new String[]{"source1", "source2"};
    // Create a buffer for reading the files
    byte[] buf = new byte[1024];
    try {
    // Create the ZIP file
    String target = "target.zip";
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
    // Compress the files
    for (int i=0; i<source.length; i++) {
    FileInputStream in = new FileInputStream(source);
    // Add ZIP entry to output stream.
    out.putNextEntry(new ZipEntry(source[i]));
    // Transfer bytes from the file to the ZIP file
    int len;
    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    // Complete the entry
    out.closeEntry();
    in.close();
    // Complete the ZIP file
    out.close();
    } catch (IOException e) {

  • 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)

  • A strange problem using the zip files

    I'm currently working on a university project with another student in which we use zip files (using the java ZipFile , and ZipOutputStream classes), we write certain data to a zip file (and verify that it is there by checking the disk). and later we read the zip file, and write it back , appending some data at the end of the file.
    although we see the changes actually take place on the disk , in one of the cases when reading the data we don't see the data we have appended before (although it is physically written on the disk).
    testing has revealed that this has something to do with the rename command or java being unsynchronized with the data actually on the disk (caching problem?).
    we use this code to preform the process:
    ZipFile zf= new ZipFile( ((Integer)lexicon.get(keyword)).intValue()+".zip");
              BufferedInputStream input= new BufferedInputStream(zf.getInputStream(new ZipEntry("data")));
              // create a temporary new zip file.
              ZipOutputStream zipst= new ZipOutputStream(new FileOutputStream( ((Integer)lexicon.get(keyword)).intValue()+".tmp.zip"));
              BufferedOutputStream output =new BufferedOutputStream(zipst);
              zipst.putNextEntry(new ZipEntry("data"));
              // stream the contents of the old file into the new file
              //(we do it this way because of the inability of the java zip file interfaces to add to an existing zip file)
              int current = input.read();
              while(current!=-1){
              output.write(current);
              current = input.read();
              output.write(entry.getBytes(false)); // add the new entrys.
              output.flush();
              zipst.closeEntry();
              output.close();
              // erase the old file.
              File toErase = new File( ((Integer)lexicon.get(keyword)).intValue()+".zip");
              toErase.delete();
              // rename the temporary file to the old files name.
              File toRename = new File( ((Integer)lexicon.get(keyword)).intValue()+".tmp.zip");
              toRename.renameTo(new File( ((Integer)lexicon.get(keyword)).intValue()+".zip"));
    we use:
    1. j2sdk 1.4.2_05
    2. running under libranet linux.
    any help will be appreciated

    the data does indeed get lost in the course fo rewriting the file, e assume this is some sort of synchronization issue with java and the file system.
    addig enteies to an existing zip file would have been great, if we had it a few weeks ago , we had to make do without this ability , and all of the code is already written and mostly working (except for the problem I mention) and we don't really want to rewrite the zip file access.
    additional testing has shown that the data gets lost when we use the rename command.
    very strange...
    thanks for the input

  • Sending HR-File as email by the ABAP program as password protected ZIP file

    Hi All,
    My requiremet is to directly email the SAP-HR files to the users as the password protected ZIP file on UNIX.
    Can anyone help me out how to implement this in my ABAP program.
    Regards,
    Saumik

    hi,
    To populate data in different column you may use the below code.
    DATA : filename TYPE string VALUE  "Path
    DATA :BEGIN OF wa_string,
                   data TYPE string,
              END OF wa_string.
    DATA : it_data LIKE STANDARD TABLE OF wa_string,
               data  TYPE string.
    DATA: v_tab TYPE char1.
    v_tab = cl_abap_char_utilities=>horizontal_tab.
    CONCATENATE 'happy' 'new year'  INTO wa_string-data SEPARATED BY v_tab.
    APPEND wa_string TO it_data .
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename              = filename
      TABLES
        data_tab              = it_data.
    IF sy-subrc <> 0.
    ENDIF.

  • Setting the name of streamed zip file

    Hi,
    I have a servlet which creates a zip file with documents retrieved from a database. To create the zip file I am using the ZipOutputStream class and adding the documents one at a time to this object. This is all working fine.
    The problem I have is pretty simple, in that I can't seem to find a way in which to set the name of the zip file. This must be possible! At the moment it appears to be setting the name of the zip file to the name of the class that created it. In this case my class is called DocumentPacker and what comes back from the servlet is a zip file called DocumentPacker.zip. This wouldn't be so bad, but if you're creating the zip file for a user that does not have cookies enabled, it appends the session id to the end of the zip file name which i don't want.
    I am creating my ZipOutputStream like this:
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
    and then adding the documents one at a time using a BufferedInputStream.).
    Any help on how to set the name would be much appreciated
    Thanks
    Claire

    Your servlet must set some headers on the HTTP response (Content-Type as "application/zip", Content-Disposition as "attachment; filename=yourfilename.zip")
    Read the RFC1806 document.
    Content-Type: application/zip
    Content-Disposition: attachment; filename=genome.jpeg

Maybe you are looking for