Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8848323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:23:52+00:00 2026-06-14T12:23:52+00:00

well my question is really simple, is about an unexpected behavior (or at least

  • 0

well my question is really simple, is about an unexpected behavior (or at least is unexpected to me) while I try to zip a directory, I have the following methods that I’ve created on my own (I’m quite aware that I’m not handling exceptions and all that stuff, It is because (by now) I’m just doing this to learn how to do it so stability “is not really important”), here is the code:

public static void zipDirectory(File srcDirectory, File zipFile) throws IllegalArgumentException {
    if (!srcDirectory.isDirectory()) {
        throw new IllegalArgumentException("The first parameter (srcDirectory) MUST be a directory.");
    }
    int bytesRead;
    byte[] dataRead  = new byte[1000];
    BufferedInputStream in = null;
    ZipOutputStream zOut;
    try {
        zOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
        for (File f : srcDirectory.listFiles()) {
            if (f.isDirectory()) {
                FileUtilities.zipInnerDirectory(f,zOut);
            }else {
                in = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()), 1000);
                zOut.putNextEntry(new ZipEntry(f.getPath())); 
                while((bytesRead = in.read(dataRead,0,1000)) != -1) {
                    zOut.write(dataRead, 0, bytesRead);
                }
                zOut.closeEntry();
            }
        }
        zOut.flush();
        zOut.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static void zipInnerDirectory(File dir, ZipOutputStream zOut) throws IllegalArgumentException {
    if (!dir.isDirectory()) {
        throw new IllegalArgumentException("The first parameter (srcDirectory) MUST be a directory.");
    }
    BufferedInputStream in = null;
    int bytesRead;
    byte[] dataRead  = new byte[1000];
    try {
        for (File f : dir.listFiles()) {
            if (f.isDirectory()) {
                FileUtilities.zipInnerDirectory(f,zOut);
            }else {
                in = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()), 1000);
                zOut.putNextEntry(new ZipEntry(f.getPath())); 
                while((bytesRead = in.read(dataRead,0,1000)) != -1) {
                    zOut.write(dataRead, 0, bytesRead);
                }
                zOut.closeEntry();
            }
        }
        zOut.flush();
        zOut.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

As I said is not my best coding so please don’t judge the code (or at least don’t be too strict 😉 ), I know it can be so much better; ok the “unexpected behavior” is this, let’s say that I have the following directory:

  • H:\MyDir1\MyDir2\MyDirToZip

when i send as a parameter a file created with that path (new File("H:\\MyDir1\\MyDir2\\MyDirToZip")) everything’s work pretty fine the zip is created successfully, the thing is that when I open (unzip) the files inside the zip they have the next structure:

  • H:\MyDir1\MyDir2\MyDirToZip

when I was expecting to find inside just:

  • \MyDirToZip

without H: \MyDir1 \MyDir2 which are “unnecessary” (BTW they just contain one to each other in the appropriate order, i mean, the other files that are in them are not compressed, that is why I say they are unnecessary) so the question is, what I’m I doing wrong? how can I specify that I just want to zip the structure down the srcDirectory?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:23:53+00:00Added an answer on June 14, 2026 at 12:23 pm
    zOut.putNextEntry(new ZipEntry(f.getPath())); 
    

    This should be the problem. f.getPath() will return a path that’s relative to some root directory (probably your current working dir), but not relative to the directory you are zipping. You need to figure out a way to get the relative path from the zip directory, possibly this will do:

    new ZipEntry(f.getAbsolutePath().substring(zipDir.getAbsolutePath().length()))
    

    or, if you want the root directory added:

    new ZipEntry(zipDir.getName() + "/"
                 + f.getAbsolutePath().substring(zipDir.getAbsolutePath().length()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a simple Design question and am not really sure about pro´s and con´s.
Hello I have a question about matching groups based on the following regular expression
I am experimenting with fluent extension methods. I have the following simple extension method
This is probably a really newbie question (well, I'm pretty sure it is), but
Well! I feel really stupid for this question, and I wholly don't mind if
Sorry this is not a very well defined question, I am thinking about an
As well as my question Removing MKMapView Annotations causes leaks. I have discovered that
i have this PHP code, and a DataBase with Question, answer1, answer2, Question_id well,
Well looks too simple a question to be asked but i asked after going
The question seems pretty well formulated I have a virtual machine which implements only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.