[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Create Zip file for Mac while preserving folder directory structure

Posted on 2004-10-26
6
Medium Priority
?
613 Views
Last Modified: 2008-02-01

Hi,
In my application I am zipping up a directory structure on the server (Linux or PC) and providing a link for the user to download the zip file.  It is important that when the user extract the files the full tree is preserved.  I am using java.util.zip.ZipEntry & java.util.zip.ZipOutputStream to do this.  This works fine with WinZip on PC.   But on Macs (a sizeable chunk of the user), when StuffIt extract the file, the directory names get merged as part of the file name.  So I get only a single folder with lots of files with long names (e.g. "D:\xxx\yyy\dd\hello.txt").  When I use tar and gzip on unix to create the file this does not happen, folder structures are preserved.  But when I use java this happens.  How do I get around this?  I am new to creating zip files with java so please be detailed in your explaination.

I am also looking for ways to creat zip file with "relative" path rather than "absolute" but haven't figure that out yet.  My code is running under Tomcat 4.1.  Could this be my problem?

 Here's the main part of my code:


  public boolean executeZip() throws
      IOException {

    String inputFiles = "D:\\download\\InDir\\08_24_ID5551212";
    String outZipFileName = "TestZip.zip";
    String outputZipFilePath = "D:\\download\\OutDir" + "\\" + outZipFileName;
   
    System.out.println("Zipping files in " + inputFiles);
   

    try {
      System.out.println("Output file path:" + outputZipFilePath);
      //create a ZipOutputStream to zip the data to
      ZipOutputStream zos = new
          ZipOutputStream(new FileOutputStream(outputZipFilePath));
     
      zipDir(inputFiles, zos);
      //close the stream
      zos.close();
      File file = new File(outputZipFilePath);

      // Get the number of bytes in the file
      outputZipFileSize = file.length() / 1024;

    }
    catch (InterruptedException e) {
      setRunning(false);
      return false;
    }
    catch (Exception e) {
      //handle exception
      System.out.println(e);
      return false;
    }

    return true;
  }

  public void zipDir(String dir2zip, ZipOutputStream zos) throws Exception {

    //create a new File object based on the directory we have to zip File
    File zipDir = new File(dir2zip);
    //get a listing of the directory content
    String[] dirList = zipDir.list();
    byte[] readBuffer = new byte[2156];
    int bytesIn = 0;
    //loop through dirList, and zip the files
    for (int i = 0; i < dirList.length; i++) {
      File f = new File(zipDir, dirList[i]);
      if (f.isDirectory()) {
        //if the File object is a directory, call this
        //function again to add its content recursively
        String filePath = f.getPath();
        zipDir(filePath, zos);
        //loop again
        continue;
      }
      //if we reached here, the File object f was not a directory
      //create a FileInputStream on top of f
      FileInputStream fis = new FileInputStream(f);
      //create a new zip entry
      ZipEntry anEntry = new ZipEntry(f.getPath());
      //place the zip entry in the ZipOutputStream object
      zos.putNextEntry(anEntry);
      //now write the content of the file to the ZipOutputStream
      while ( (bytesIn = fis.read(readBuffer)) != -1) {
        zos.write(readBuffer, 0, bytesIn);
      }
      //close the Stream
      fis.close();
    }
  }
0
Comment
Question by:tangowayne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12418466
You can truncate the path part of the ZipEntry. I.e., the path the ZipEntry has is the path with which it is stored in the Zip file. Before creating the ZipEntry, substring the file system path to something appropriate for your application.

\t
0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 100 total points
ID: 12418487
Also, it would be a good idea to change all back slashes to forward slashes. Forward slashes are handled correctly on all platforms.

\t
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 100 total points
ID: 12419570
Hi tangowayne,

As orangehead911 said :
>> Also, it would be a good idea to change all back slashes to forward slashes.
but the actual reason is zip files only supports '/' as file separator.

>> ZipEntry anEntry = new ZipEntry(f.getPath());
maybe you don't want the fullpath in the zip file, but a relative one :
String fpath=f.getPath();
if (fpath.startsWith(abs_base_dir)) fpath=fpath.substring(abs_base_dir.length());
ZipEntry anEntry = new ZipEntry(fpath);




0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tangowayne
ID: 12437733
Thanks Webstorm and orangehead911.  It makes sense to me now and it's working beautifully.

0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12438556
=-)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12447219
:-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question