[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1473
  • Last Modified:

Java Zip directory with subdirectories without absolute paths

Hello,

I want to zip this directory:

mydir

The abolute path is:

C:\dir1\dir2\dir3\mydir

When I unzip the file I created, I get:

dir1\dir2\dir3\mydir

But I just want:

mydir

Can someone help me change my code to get this effect? Thanks
package test;

import java.io.*;
import java.util.zip.*;

/**
 * Compresses a file or directory into a Zip archive. Users of the
 * class supply the name of the file or directory as an argument.
 */
public class SimpleZip {

   private static ZipOutputStream zos;

   public static void main(String[] args) {
      
	   
	   
	   //User must specify a directory to compress
      if (args.length < 1) {
         System.out.println("Usage: java SimpleZip directoryName");
         System.exit(0);
      }
      //Get the name of the file or directory to compress.
      String fileName = args[0];
      //Use the makeZip method to create a Zip archive.
      try {
         makeZip(fileName);
      }
      //Simply print out any errors we encounter.
      catch (Exception e) {
         System.err.println(e);
      }
   }

   /**
    * Creates a Zip archive. If the name of the file passed in is a
    * directory, the directory's contents will be made into a Zip file.
    */
   public static void makeZip(String fileName)
         throws IOException, FileNotFoundException
   {
      File file = new File(fileName);
      zos = new ZipOutputStream(new FileOutputStream("C:\\Test.zip"));
      //Call recursion.
      recurseFiles(file);
      //We are done adding entries to the zip archive,
      //so close the Zip output stream.
      zos.close();
   }

   /**
    * Recurses down a directory and its subdirectories to look for
    * files to add to the Zip. If the current file being looked at
    * is not a directory, the method adds it to the Zip file.
    */
   private static void recurseFiles(File file)
      throws IOException, FileNotFoundException
   {
      if (file.isDirectory()) {
         //Create an array with all of the files and subdirectories
         //of the current directory.
         String[] fileNames = file.list();
         if (fileNames != null) {
            //Recursively add each array entry to make sure that we get
            //subdirectories as well as normal files in the directory.
            for (int i=0; i<fileNames.length; i++)  {
               recurseFiles(new File(file, fileNames[i]));
            }
         }
      }
      //Otherwise, a file so add it as an entry to the Zip file.
      else {
         byte[] buf = new byte[1024];
         int len;
         //Create a new Zip entry with the file's name.
         ZipEntry zipEntry = new ZipEntry(file.toString());
         //Create a buffered input stream out of the file
         //we're trying to add into the Zip archive.
         FileInputStream fin = new FileInputStream(file);
         BufferedInputStream in = new BufferedInputStream(fin);
         zos.putNextEntry(zipEntry);
         //Read bytes from the file and write into the Zip archive.
         while ((len = in.read(buf)) >= 0) {
            zos.write(buf, 0, len);
         }
         //Close the input stream.
         in.close();
         //Close this entry in the Zip stream.
         zos.closeEntry();
      }
   }
}

Open in new window

0
jrwalker2
Asked:
jrwalker2
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
Gurvinder Pal SinghCommented:
0
 
a_bCommented:
Try changing the current directory -

http://www.antwerkz.com/changing-the-current-directory/
0
 
CEHJCommented:
What are you going to do if there are files, even with clashing names, in the higher directories?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jrwalker2Author Commented:
I tried the links and the zip file results in the same as what my code already does. Can someone please help me fix my code? Thanks
0
 
CEHJCommented:
You didn't answer my question ...
0
 
jrwalker2Author Commented:
There are no repeat file names in the higher directory, so that is not an issue. Can you help me fix the code please?
0
 
Gurvinder Pal SinghCommented:
@jrwalker2: did you tried the link that i had posted?

Thanks
0
 
CEHJCommented:
Programmatically doing what you want to do is actually easier than preserving the structure. All you need to do is iterate the ZipEntry instances and then write to your directory of choice if ze,isDirectory() == false
0
 
jrwalker2Author Commented:
HI CEHJ,

I'd like to maintain the directory structure from "mydir". From what I understand, it seems like I would end up with a zip file with just filles (no directories) if I added files only if isDirectory()==false
0
 
CEHJCommented:
OK, then you'd have to filter out all but the mydir directory
0
 
jrwalker2Author Commented:
To solve this issue, I created a jar file with the original code. Then I placed the jar file at the same level as the directory that I wanted to zip. Finally, from the code I executed the jar file.  
0
 
jrwalker2Author Commented:
I solved the issue
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now