Update on java.zip util- please please help

Discussion in 'Java' started by queen, Jul 15, 2004.

  1. queen

    queen Guest

    I am nearly at my wits end with this program and need real help.
    Ok- If you remember, I needed to copy a zip file the user specifies
    and create a copy of that zip file.
    So i cut out the middle step of extracting the files to a dir and then
    zipping them back up- rather, I created a reference to the files in
    the old zip (using enumeration type) and casting each nextElement as a
    ZipEntry, sent the files in the old zip directly to the new zip- so no
    need to delete the files anymore, b/c they are never extracted to a
    dir:

    Enumeration zipFileEntries=zipFile.entries(); //zipFile is a ZipFile

    But still have problem of empty folder-It was interesting to read what
    you guys had to say on the matter though. But for now, I'll assume
    that there are no non empty subfolders in the zip. I can't deal with
    that now. I have a new problem.

    my code to add the files from the old zip to the new zip:

    while(zipFileEntries.hasMoreElements())
    {
    //get/create a zip file entry
    ZipEntry entry=(ZipEntry)zipFileEntries.nextElement();
    /*without this, a file of unrecognizeable type is creatred bearing the
    subfolder's name*/
    if(entry.isDirectory())
    continue;
    out.putNextEntry(entry);
    copy(zipFile.getInputStream(entry), out);
    }
    out.close();

    public static void copy(InputStream in, OutputStream out) throws
    IOException
    {
    //Transfer bytes from in to out
    byte[] buf=new byte[1024];
    int len;
    //write the src file to dest file
    while ((len=in.read(buf)) > 0)
    out.write(buf, 0, len);
    in.close();
    }

    It works fine for .txt files. But when the old zip has other file
    types, ie: .doc, or .xls, then it crashes when it gets to the point of
    adding that file to the new zip-
    error I get:
    Exception in thread "main" java.util.zip.ZipException: invalid entry
    compressed size (expected 9059 but got 8996 bytes)...

    What does this mean? How can I make it work for all types of files?

    Someone? Anyone? Please please help.
    Thanks so much guys. Youre the best! :)
     
    queen, Jul 15, 2004
    #1
    1. Advertising

  2. queen

    Chris Smith Guest

    queen wrote:
    > It works fine for .txt files. But when the old zip has other file
    > types, ie: .doc, or .xls, then it crashes when it gets to the point of
    > adding that file to the new zip-
    > error I get:
    > Exception in thread "main" java.util.zip.ZipException: invalid entry
    > compressed size (expected 9059 but got 8996 bytes)...


    There are all sorts of missing information here. For example:

    1. What's in the "..." part of the error message? That's the part
    that tells you where the error is, and I'm not sure why you
    deleted it, apparently on purpose.

    2. What's the implementation of your copy method?

    There should be no reason that text files are any different from any
    other file, unless your copy method is wrong.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 15, 2004
    #2
    1. Advertising

  3. queen

    Roedy Green Guest

    On 14 Jul 2004 16:09:18 -0700, (queen) wrote or
    quoted :

    >out.putNextEntry(entry);
    >copy(zipFile.getInputStream(entry), out);


    you need out.closeEntry();
    after your copy.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 15, 2004
    #3
  4. queen

    queen Guest

    Chris Smith <> wrote in message news:<>...
    > There are all sorts of missing information here. For example:
    >
    > 1. What's in the "..." part of the error message? That's the part
    > that tells you where the error is, and I'm not sure why you
    > deleted it, apparently on purpose.
    >
    > 2. What's the implementation of your copy method?
    >
    > There should be no reason that text files are any different from any
    > other file, unless your copy method is wrong.


    Sorry- :)
    1) Exception in thread "main" java.util.zip.ZipException: invalid
    entry
    compressed size (expected 9059 but got 8996 bytes)
    at java.util.zip.ZipOutputStream.closeEntry(Unknown Source)
    at java.util.zip.ZipOutputStream.putNextEntry(Unknown Source)
    at ZipProgram.addToZip(ZipProgram.java:109)
    at ZipProgram.main(EachFile.java:178)

    2) Here is a more complete form of the code. This part is broken down
    into 3 functions: a) to get the zip entries from oldZipFile.zip b) add
    each file to ZipOutputStream (newZipFile.zip) and this calls, for each
    file 3) copy- to copy the file.
    And you are right :) about no difference btwn .txt or other type
    file- when I ran it again it crashed as it reached a .txt.
    However, if there were only .txt in the zip it would run fine- only
    crashes when I add other files types.
    -The 3 functions:
    1) getZipEntries(String zf) //gets the contents of oldZipFile.zip
    {
    File z=new File(zf);
    ZipFile zipFile=new ZipFile(z, ZipFile.OPEN_READ);
    Enumeration zipFileEntries=zipFile.entries(); //get entries of
    oldZipFile.zip
    addToZip();
    }

    2) addToZip()
    {
    while(zipFileEntries.hasMoreElements())
    {
    ZipEntry ze=(ZipEntry)zipFileEntries.nextElement();
    zos.putNextEntry(ze); //zos is type ZipOutputStream
    copy(zipFile.getInputStream(ze),zos);
    }
    zos.close();
    }

    3) copy(InputStream in, OutputStream out)
    {
    byte []buf=new byte[1024];
    int len;
    while((len=in.read(buf))>0)
    out.write(buf,0,len);
    in.close();
    }
    }

    Please can you help?
     
    queen, Jul 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    1
    Views:
    605
    Andrew Thompson
    Sep 16, 2004
  2. yogesh
    Replies:
    4
    Views:
    5,066
    anish.mathew84
    Dec 30, 2009
  3. Replies:
    0
    Views:
    1,740
  4. HARI OM
    Replies:
    0
    Views:
    17,433
    HARI OM
    May 3, 2005
  5. sunil_varma
    Replies:
    1
    Views:
    1,192
Loading...

Share This Page