Java Zip Folder zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir)

Here you can find the source of zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir)

Description

zip Dir

License

Open Source License

Declaration

protected static void zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir) throws IOException 

Method Source Code


//package com.java2s;
/*//  www .  ja  v  a  2 s .c om
 *  Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    protected static void zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir) throws IOException {
        //get a listing of the directory content
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[40960];
        int bytesIn;
        //loop through dirList, and zip the files
        if (dirList != null) {
            for (String aDirList : dirList) {
                File f = new File(zipDir, aDirList);
                //place the zip entry in the ZipOutputStream object
                zos.putNextEntry(new ZipEntry(getZipEntryPath(f, archiveSourceDir)));
                if (f.isDirectory()) {
                    //if the File object is a directory, call this
                    //function again to add its content recursively
                    zipDir(f, zos, archiveSourceDir);
                    //loop again
                    continue;
                }
                //if we reached here, the File object f was not a directory
                //create a FileInputStream on top of f
                try (FileInputStream fis = new FileInputStream(f)) {
                    //now write the content of the file to the ZipOutputStream
                    while ((bytesIn = fis.read(readBuffer)) != -1) {
                        zos.write(readBuffer, 0, bytesIn);
                    }
                }
            }
        }
    }

    protected static String getZipEntryPath(File f, String archiveSourceDir) {
        String entryPath = f.getPath();
        entryPath = entryPath.substring(archiveSourceDir.length() + 1);
        if (File.separatorChar == '\\') {
            entryPath = entryPath.replace(File.separatorChar, '/');
        }
        if (f.isDirectory()) {
            entryPath += "/";
        }
        return entryPath;
    }
}

Related

  1. zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start)
  2. zipDir(File dir, ZipOutputStream zos, String prefix)
  3. zipDir(File directory, ZipOutputStream zos, String path, Set exclusions)
  4. zipDir(File sourceDir, File destFile)
  5. zipDir(File zipDir, ZipOutputStream zos, boolean recurse)
  6. zipDir(File zipDir, ZipOutputStream zos, String basePath)
  7. zipDir(File zipDir, ZipOutputStream zos, String name)
  8. zipDir(File zipFile, File dir, boolean includeRoot)
  9. zipDir(File zipFile, File dirObj)