Zipping a directory

Discussion in 'Java' started by Kasper K, Dec 22, 2003.

  1. Kasper K

    Kasper K Guest

    Hi,

    Simple question. I want to zip a directory using
    java.util.zip.ZipOutputStream. I use the code below. My problem is
    that ZipEntry dosnt seems to like the danish chars "æ ø å". So when I
    unzip my compressed directory the files containing those chars has
    substituded them with something like "-(_/&%".

    private void zipDir(File dir, ZipOutputStream out) throws Exception
    {
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];
    File files[] = dir.listFiles();

    for (int i=0; i<files.length; i++)
    {
    if(files.isDirectory())
    {
    zipDir(files, out);
    } else
    {
    FileInputStream fi = new FileInputStream(files);
    BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);

    String name = files.getPath();
    name = name.substring(3);
    //System.out.println(name);

    ZipEntry entry = new ZipEntry(name);
    out.putNextEntry(entry);
    int count;
    while((count = origin.read(data, 0, BUFFER)) != -1)
    {
    out.write(data, 0, count);
    }
    origin.close();
    out.closeEntry();
    }
    }
    }

    Kasper, Denmark
     
    Kasper K, Dec 22, 2003
    #1
    1. Advertising

  2. Kasper K

    Harald Hein Guest

    "Kasper K" wrote:

    > Simple question. I want to zip a directory using
    > java.util.zip.ZipOutputStream. I use the code below. My problem is
    > that ZipEntry dosnt seems to like the danish chars "æ ø å". So
    > when I unzip my compressed directory the files containing those
    > chars has substituded them with something like "-(_/&%".


    Your description is unclear (is the file content or the file name
    corrupted). So just some hints:

    Get a debugger and figure out if the data gets messed up during
    compression or uncompression.

    Check the zip standard. You will find that only 8 bit chars are
    officially supported. Sun does a UTF-8 encoding for other chars. If you
    use an unzipper which isn't aware of this, you get strange characters.

    Check Sun's bugparade. There was an issue with incorrect UTF-8 encoding
    /decoding of file names in the zip package.


    > String name = files.getPath();
    > name = name.substring(3);


    ? Workaround for Windows? if yes, check the File class for better
    methods.
     
    Harald Hein, Dec 22, 2003
    #2
    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. Tyler Reed

    Zipping without a file

    Tyler Reed, Sep 16, 2004, in forum: Java
    Replies:
    5
    Views:
    377
    Thomas Schodt
    Sep 16, 2004
  2. Replies:
    1
    Views:
    3,943
    Andrey Kuznetsov
    Nov 11, 2005
  3. Doug Tolton

    Zipping and Unzipping files

    Doug Tolton, Nov 21, 2003, in forum: Python
    Replies:
    1
    Views:
    320
    Peter Hansen
    Nov 21, 2003
  4. OriginalBrownster

    Zipping Files to user defined Directory

    OriginalBrownster, Jul 28, 2006, in forum: Python
    Replies:
    4
    Views:
    336
    Danny Milosavljevic
    Sep 2, 2006
  5. Raju Aralikatti

    creating directory and zipping it

    Raju Aralikatti, Jan 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    183
    Andrew Timberlake
    Jan 8, 2008
Loading...

Share This Page