Java Zip Folder zipFolder2(ZipOutputStream zos, String folderName, String baseFolderName)

Here you can find the source of zipFolder2(ZipOutputStream zos, String folderName, String baseFolderName)

Description

zip Folder

License

Open Source License

Declaration

public static void zipFolder2(ZipOutputStream zos, String folderName, String baseFolderName)
            throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w  ww.j a  va  2  s.  c o  m*/
 * 
 * Copyright (C) 2013
 * 
 * This file is part of Messic.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.util.List;

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

public class Main {
    public static void zipFolder2(ZipOutputStream zos, String folderName, String baseFolderName)
            throws IOException {
        File f = new File(folderName);
        if (f.exists()) {

            if (f.isDirectory()) {
                // Thank to peter
                // For pointing out missing entry for empty folder
                if (!folderName.equalsIgnoreCase(baseFolderName)) {
                    String entryName = folderName.substring(baseFolderName.length(), folderName.length())
                            + File.separatorChar;
                    ZipEntry ze = new ZipEntry(entryName);
                    zos.putNextEntry(ze);
                }
                File f2[] = f.listFiles();
                for (int i = 0; i < f2.length; i++) {
                    zipFolder2(zos, f2[i].getAbsolutePath(), baseFolderName);
                }
            } else {
                // add file
                // extract the relative name for entry purpose
                String entryName = folderName.substring(baseFolderName.length(), folderName.length());
                ZipEntry ze = new ZipEntry(entryName);
                zos.putNextEntry(ze);
                FileInputStream in = new FileInputStream(folderName);
                int len;
                byte buffer[] = new byte[1024];
                while ((len = in.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
                in.close();
                zos.closeEntry();

            }
        } else {
            throw new IOException("File or directory not found " + folderName);
        }

    }

    /**
     * List all the files that exist in a certain path (and subfolders)
     * 
     * @param basePath {@link String} absolute path to a directory to start searching
     * @param files {@link List}<File/> a list that will be filled with the existing files in the path
     */
    public static final void listFiles(String basePath, List<File> files) {
        File directory = new File(basePath);

        // get all the files from a directory
        File[] fList = directory.listFiles();
        if (fList != null) {
            for (File file : fList) {
                if (file.isFile()) {
                    files.add(file);
                } else if (file.isDirectory()) {
                    listFiles(file.getAbsolutePath(), files);
                }
            }
        }
    }
}

Related

  1. zipFolder(String srcFolder, String destZipFile)
  2. zipFolder(String srcFolder, String destZipFile)
  3. zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder)
  4. zipFolder(String srcFolderPath, String outputZipPath)
  5. zipFolder(ZipOutputStream zipOut, File folder, File root)