troubleshooting Question

Java Zip directory with subdirectories without absolute paths

Avatar of jrwalker2
jrwalker2 asked on
Programming Languages-OtherJava
12 Comments2 Solutions1550 ViewsLast Modified:

I want to zip this directory:


The abolute path is:


When I unzip the file I created, I get:


But I just want:


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


 * 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");
      //Get the name of the file or directory to compress.
      String fileName = args[0];
      //Use the makeZip method to create a Zip archive.
      try {
      //Simply print out any errors we encounter.
      catch (Exception 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:\\"));
      //Call recursion.
      //We are done adding entries to the zip archive,
      //so close the Zip output stream.

    * 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);
         //Read bytes from the file and write into the Zip archive.
         while ((len = >= 0) {
            zos.write(buf, 0, len);
         //Close the input stream.
         //Close this entry in the Zip stream.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros