Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zipping a folder which contains subfolders

Tags:

java

zip

public static void main(String argv[]) {
    try {
        String date = new java.text.SimpleDateFormat("MM-dd-yyyy")
                .format(new java.util.Date());
        File inFolder = new File("Output/" + date + "_4D");
        File outFolder = new File("Output/" + date + "_4D" + ".zip");
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                new FileOutputStream(outFolder)));
        BufferedInputStream in = null;
        byte[] data = new byte[1000];
        String files[] = inFolder.list();
        for (int i = 0; i < files.length; i++) {
            in = new BufferedInputStream(new FileInputStream(
                    inFolder.getPath() + "/" + files[i]), 1000);
            out.putNextEntry(new ZipEntry(files[i]));
            int count;
            while ((count = in.read(data, 0, 1000)) != -1) {
                out.write(data, 0, count);
            }
            out.closeEntry();
        }
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I'm trying to zip a folder which contains subfolders. Trying to zip the folder named 10-18-2010_4D.The above program ends with the following exception. Please advise on how to clear the issue.

java.io.FileNotFoundException: Output\10-18-2010_4D\4D (Access is denied)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at ZipFile.main(ZipFile.java:17)
like image 596
LGAP Avatar asked Oct 18 '10 16:10

LGAP


People also ask

Can you zip folders with subfolders?

Creating a zip folder allows files to be organized and compressed to a smaller file size for distribution or saving space. Zip folder can have subfolders within this main folder.

How do I zip all subfolders?

To add all files in the folder and subfolder, we can use the -r command, which will compress all the files in the folder. The above code will zip all the files recursively in folder_1. The folder structure is maintained in the zip file. We can also combine multiple folders and files by recursively zipping files.

How do I zip a folder recursively?

-r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

Can a zip file contain folders?

A zip file on your computer takes up less space than the original file, meaning you can store folders as zips and extract them when needed.

How do I add a zip file to a folder?

cd folder zip -r ../zipped_folder.zip . I use -r option, the "zip filename" and the list of items I want to add into it. Refering to --help option we get: You can go into folder that contains the files and directories you want to zip in. The run the command: zip -r filename.zip ./*

What is a zip file?

Zip is the most widely used archive file format that supports lossless data compression. A Zip file is a data container containing one or more compressed files or directories.

How to zip files and directories in Linux?

Zip files can be easily extracted in Windows, macOS, and Linux using the utilities available for all operating systems. In this tutorial, we will show you how to Zip (compress) files and directories in Linux using the zip command. zip is a command-line utility that helps you create Zip archives.

How to traverse the whole directory structure recursively in a zip file?

The -r option allows you to traverse the whole directory structure recursively: You can also add multiple files and directories in the same archive: The default compression method of Zip is deflate. If the zip utility determines that a file cannot be compressed, it simply stores the file in the archive without compressing it using the store method.


2 Answers

Here's the code for creating the ZIP archive. Created archive preserves original directory structure (if any).

public static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parrentDirectoryName) throws Exception {
    if (fileToZip == null || !fileToZip.exists()) {
        return;
    }

    String zipEntryName = fileToZip.getName();
    if (parrentDirectoryName!=null && !parrentDirectoryName.isEmpty()) {
        zipEntryName = parrentDirectoryName + "/" + fileToZip.getName();
    }

    if (fileToZip.isDirectory()) {
        System.out.println("+" + zipEntryName);
        for (File file : fileToZip.listFiles()) {
            addDirToZipArchive(zos, file, zipEntryName);
        }
    } else {
        System.out.println("   " + zipEntryName);
        byte[] buffer = new byte[1024];
        FileInputStream fis = new FileInputStream(fileToZip);
        zos.putNextEntry(new ZipEntry(zipEntryName));
        int length;
        while ((length = fis.read(buffer)) > 0) {
            zos.write(buffer, 0, length);
        }
        zos.closeEntry();
        fis.close();
    }
}

Don't forget to close output streams after calling this method. Here's the example:

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:\\Users\\vebrpav\\archive.zip");
    ZipOutputStream zos = new ZipOutputStream(fos);
    addDirToZipArchive(zos, new File("C:\\Users\\vebrpav\\Downloads\\"), null);
    zos.flush();
    fos.flush();
    zos.close();
    fos.close();
}
like image 170
Bane Avatar answered Sep 17 '22 11:09

Bane


You need to check if the file is a directory because you can't pass directories to the zip method.

Take a look at this page which shows how you can recursively zip a given directory.

like image 31
dogbane Avatar answered Sep 21 '22 11:09

dogbane