Java ZipOutputStream Write zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)

Here you can find the source of zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)

Description

zip

License

Open Source License

Declaration

private static void zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)
            throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008-2011 Chair for Applied Software Engineering,
 * Technische Universitaet Muenchen.//from ww w  . j a  v  a2  s .c o m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 ******************************************************************************/

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 void zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)
            throws IOException {
        if (current.isDirectory()) {
            for (File file : current.listFiles()) {
                if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
                    zip(file, rootPath, zipStream, buffer);
                }
            }
        } else if (current.isFile()) {
            zipStream.putNextEntry(new ZipEntry(current.getPath().replace(rootPath, "")));
            FileInputStream file = new FileInputStream(current);
            int read;
            while ((read = file.read(buffer)) != -1) {
                zipStream.write(buffer, 0, read);
            }
            zipStream.closeEntry();
        } else {
            throw new IllegalStateException();
        }
    }
}

Related

  1. archiveFile(String relativePath, ZipOutputStream zos, File root)
  2. writeFolderEntry(ZipOutputStream zout, String relpath)
  3. zip(File[] files, String baseDir, ZipOutputStream zos)
  4. zip(final File directory, final File base, final ZipOutputStream zipOutputStream)
  5. zipAutoBase(File f, File base, ZipOutputStream out)
  6. zipFile(ZipOutputStream out, File file, String dir)