Java ZipOutputStream Write zip(final File directory, final File base, final ZipOutputStream zipOutputStream)

Here you can find the source of zip(final File directory, final File base, final ZipOutputStream zipOutputStream)

Description

zip

License

Apache License

Declaration

private static final void zip(final File directory, final File base,
            final ZipOutputStream zipOutputStream) throws IOException 

Method Source Code

//package com.java2s;
/**/*  w ww  .  j a v a 2 s  .c o m*/
 * Copyright (C) 2012 Patrice Brend'amour <p.brendamour@bitzeche.de>
 *
 * 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.*;

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

public class Main {
    private static final int ZIP_BUFFER_SIZE = 8192;

    private static final void zip(final File directory, final File base,
            final ZipOutputStream zipOutputStream) throws IOException {
        File[] files = directory.listFiles();
        byte[] buffer = new byte[ZIP_BUFFER_SIZE];
        int read = 0;
        for (int i = 0, n = files.length; i < n; i++) {
            if (files[i].isDirectory()) {
                zip(files[i], base, zipOutputStream);
            } else {
                FileInputStream fileInputStream = new FileInputStream(
                        files[i]);
                ZipEntry entry = new ZipEntry(getRelativePathOfZipEntry(
                        files[i], base));
                zipOutputStream.putNextEntry(entry);
                while (-1 != (read = fileInputStream.read(buffer))) {
                    zipOutputStream.write(buffer, 0, read);
                }
                fileInputStream.close();
            }
        }
    }

    private static String getRelativePathOfZipEntry(final File fileToZip,
            final File base) {
        String relativePathOfFile = fileToZip.getPath().substring(
                base.getPath().length() + 1);
        if (File.separatorChar != '/') {
            relativePathOfFile = relativePathOfFile.replace(
                    File.separatorChar, '/');
        }

        return relativePathOfFile;
    }
}

Related

  1. archiveFile(String relativePath, ZipOutputStream zos, File root)
  2. writeFolderEntry(ZipOutputStream zout, String relpath)
  3. zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)
  4. zip(File[] files, String baseDir, ZipOutputStream zos)
  5. zipAutoBase(File f, File base, ZipOutputStream out)
  6. zipFile(ZipOutputStream out, File file, String dir)
  7. zipFile(ZipOutputStream out, File sourceFile)
  8. zipFile(ZipOutputStream out, String stripPath, File file, char pathSeparator)