Java Zip Files addFileToZip(ZipOutputStream zos, File file, File rootDir)

Here you can find the source of addFileToZip(ZipOutputStream zos, File file, File rootDir)

Description

add File To Zip

License

Open Source License

Declaration

private static void addFileToZip(ZipOutputStream zos, File file, File rootDir) throws IOException 

Method Source Code


//package com.java2s;
/*//from w ww. j  av a 2s.com
     
Copyright (C)    2007 Joao F. (joaof@sourceforge.net)
             http://paccman.sourceforge.net 
    
This program is free software; you can redistribute it and/or modify      
it under the terms of the GNU General Public License as published by      
the Free Software Foundation; either version 2 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 General Public License for more details.                              
    
You should have received a copy of the GNU General Public License         
along with this program; if not, write to the Free Software               
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
     
*/

import java.io.BufferedInputStream;

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 {
    private static final int BUF_SIZE = 256 * 1024;

    private static void addFileToZip(ZipOutputStream zos, File file, File rootDir) throws IOException {
        if (file.isFile()) {
            File relativeFile = new File(file.getAbsolutePath().substring(rootDir.getPath().length() + 1));
            ZipEntry ze = new ZipEntry(relativeFile.getPath());
            zos.putNextEntry(ze);
            int count;
            byte data[] = new byte[BUF_SIZE];
            BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file));
            while ((count = origin.read(data, 0, BUF_SIZE)) != -1) {
                zos.write(data, 0, count);
            }
            origin.close();
        } else {
            for (File f : file.listFiles()) {
                addFileToZip(zos, f, rootDir);
            }
        }
    }
}

Related

  1. addFileToZip(String path, String srcFile, ZipOutputStream zip)
  2. addFileToZip(String path, String srcFile, ZipOutputStream zip)
  3. addFileToZip(ZipOutputStream out, InputStream in, String entry)
  4. addFileToZip(ZipOutputStream zipOutputStream, File file, String basePath)
  5. addFileToZip(ZipOutputStream zipOutputStream, String path, byte[] bytes)
  6. addFileToZipOutputStream(File file, ZipOutputStream zipOs)
  7. addToZip(byte[] zip, String file, String fileName)
  8. addToZip(File directoryToZip, File file, ZipOutputStream zos)
  9. addToZip(File f, int truncate, ZipOutputStream os, byte[] buff)