Java Zip Folder zipDir(String dir2zip, ZipOutputStream zos)

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

Description

zip Dir

License

Open Source License

Declaration

public static void zipDir(String dir2zip, ZipOutputStream zos) 

Method Source Code


//package com.java2s;
/*/*from   ww  w. j a v  a  2s.c o m*/
*  Nokia Data Gathering
*
*  Copyright (C) 2011 Nokia Corporation
*
*  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 2.1 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.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipDir(String dir2zip, ZipOutputStream zos) {
        try {
            File zipDir = new File(dir2zip);

            String[] dirList = zipDir.list();
            byte[] readBuffer = new byte[2156];
            int bytesIn = 0;

            for (int i = 0; i < dirList.length; i++) {
                File f = new File(zipDir, dirList[i]);

                if (f.isDirectory()) {
                    String filePath = f.getPath();
                    zipDir(filePath, zos);
                    continue;
                }

                FileInputStream fis = new FileInputStream(f);

                ZipEntry anEntry = new ZipEntry(f.getPath());
                zos.putNextEntry(anEntry);

                while ((bytesIn = fis.read(readBuffer)) != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                }

                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. zipDir(String baseDir, String dir2zip, ZipOutputStream zos)
  2. zipDir(String dir, ZipOutputStream zos)
  3. zipDir(String dir2zip, FilenameFilter filter, java.util.zip.ZipOutputStream zos, int bufferSize)
  4. zipDir(String dir2zip, String zipFileName)
  5. zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName)
  6. zipDir(String directory, String zipName)
  7. zipDir(String origDir, File dirObj, ZipOutputStream zos)
  8. zipFolder(File root)
  9. zipFolder(File root)