Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Zipping a directory

Reply
Thread Tools

Zipping a directory

 
 
Kasper K
Guest
Posts: n/a
 
      12-22-2003
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[i].isDirectory())
{
zipDir(files[i], out);
} else
{
FileInputStream fi = new FileInputStream(files[i]);
BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);

String name = files[i].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
 
Reply With Quote
 
 
 
 
Harald Hein
Guest
Posts: n/a
 
      12-22-2003
"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[i].getPath();
> name = name.substring(3);


? Workaround for Windows? if yes, check the File class for better
methods.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating directory and zipping it Raju Aralikatti Ruby 7 01-08-2008 11:53 AM
Zipping Files to user defined Directory OriginalBrownster Python 4 09-02-2006 04:45 PM
Zipping dir and avoiding absolute path iksrazal@terra.com.br Java 1 11-11-2005 03:56 PM
Zipping without a file Tyler Reed Java 5 09-16-2004 04:10 PM
Zipping many files -- each file into its own zip file with same name k* Computer Support 14 07-15-2003 12:26 PM



Advertisments