Uriel
Uriel

Reputation: 1385

How to zip/upzip a folder and all of its files and subdirectories using Java?

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?

Upvotes: 1

Views: 5586

Answers (1)

Kevin Day
Kevin Day

Reputation: 16383

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

Upvotes: 6

Related Questions