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 8532377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:42:06+00:00 2026-06-11T09:42:06+00:00

I was reading this to learn how to zip/unzip files using Java. I used

  • 0

I was reading this to learn how to zip/unzip files using Java. I used this to guide me and it worked great when zipping all the files inside a folder, but when I tested it with a folder containing more folders inside of it, it didn’t work, it threw the following error:

java.io.FileNotFoundException: assets (Access is denied) //assets is the name of the folder I tried to zip
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at Zip.main(Zip.java:24)

This is the class I’m using, as you will see it’s the same Code Sample 4: Zip.java class code from the previous link:

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

public class Zip {
   static final int BUFFER = 2048;
   public void zip() {
      try {
         BufferedInputStream origin = null;
         FileOutputStream dest = new 
           FileOutputStream("H:\\myfigs.zip");
         CheckedOutputStream checksum = new 
           CheckedOutputStream(dest, new Adler32());
         ZipOutputStream out = new 
           ZipOutputStream(new 
             BufferedOutputStream(checksum));
         //out.setMethod(ZipOutputStream.DEFLATED);
         byte data[] = new byte[BUFFER];
         // get a list of files from current directory
         File f = new File(".");
         String files[] = f.list();

         for (int i=0; i<files.length; i++) {
            System.out.println("Adding: "+files[i]);
            FileInputStream fi = new 
              FileInputStream(files[i]);
            origin = new 
              BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(files[i]);
            out.putNextEntry(entry);
            int count;
            while((count = origin.read(data, 0, 
              BUFFER)) != -1) {
               out.write(data, 0, count);
            }
            origin.close();
         }
         out.close();
         System.out.println("checksum: "+checksum.getChecksum().getValue());
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
} 

What changes should be made so this code can zip folders inside folder and all of its files into a zip file?

  • 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-11T09:42:08+00:00Added an answer on June 11, 2026 at 9:42 am

    The zip entry needs to specify the path of the file inside the archive. You can’t add a folder to a zip archive – you can only add the files within the folder.

    The naming convention is to use forward slashes as the path separator. If you are zipping a folder with the following files/subdirectories:

    c:\foo\bar\a.txt
    c:\foo\bar\sub1\b.txt
    c:\foo\bar\sub2\c.txt
    

    the zip entry names would be:

    a.txt
    sub1/b.txt
    sub2/c.txt
    

    So to fix your algorithm, add isDirectory() inside your for loop, and then recursively add the files in any subdirectory to the zip. Probably the best way to do this is to have a method:

    addDirectoryToZip(String prefix, File directory, ZipOutputStream out)
    

    Here’s a solution for the problem: java.util.zip – Recreating directory structure

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While reading this class BitmapFactory I noticed that almost all methods inside are static.
I was reading Learn You a Haskell's guide on the state monad , but
I am trying to learn JavaScript. After reading this page: What does ':' (colon)
I am reading this article on provider patren. Kindly guide me what do this
I would like to learn PHP and started reading this info's in the website:
I am trying to learn how to use rand_r, and after reading this question
I was reading a blog post here: http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/ public interface Actor{ Performance say(Line l);
hi guys i am just doing some reading for myself to learn java and
Reading this page , it says: ...the URI in a PUT request identifies the
Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for

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.