Java ZipOutputStream Write zipRecurse(File file, int prefixLength, ZipOutputStream zos)

Here you can find the source of zipRecurse(File file, int prefixLength, ZipOutputStream zos)

Description

zip Recurse

License

Open Source License

Declaration

private static void zipRecurse(File file, int prefixLength, ZipOutputStream zos) throws IOException 

Method Source Code

//package com.java2s;
/***************************************************************************
 * Copyright (c) 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany.
 * 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
 * //from   w  w w .  j  a  va  2  s.  c om
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/

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

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static void zipRecurse(File file, int prefixLength, ZipOutputStream zos) throws IOException {
        ZipEntry entry = null;

        try {
            String name = file.getAbsolutePath() + (file.isDirectory() ? "/" : "");
            name = name.substring(prefixLength);

            if (name.length() > 0) {
                entry = new ZipEntry(name);
                zos.putNextEntry(entry);

                if (file.isFile()) {
                    FileInputStream fis = null;

                    try {
                        fis = new FileInputStream(file);
                        copy(fis, zos, 4096);
                    } finally {
                        close(fis);
                    }
                }
            }
        } finally {
            if (entry != null) {
                zos.closeEntry();
            }
        }

        if (file.isDirectory()) {
            File[] children = file.listFiles();
            for (int i = 0; i < children.length; i++) {
                File child = children[i];
                zipRecurse(child, prefixLength, zos);
            }
        }
    }

    /**
     * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
     * 
     * @param bufferSize
     *          Size of internal buffer to use.
     */
    public static void copy(Reader input, Writer output, int bufferSize) throws IOException {
        char buffer[] = new char[bufferSize];
        int n = 0;

        while ((n = input.read(buffer)) != -1) {
            output.write(buffer, 0, n);
        }
    }

    public static void copy(InputStream input, OutputStream output, byte buffer[]) throws IOException {
        int n = 0;

        while ((n = input.read(buffer)) != -1) {
            output.write(buffer, 0, n);
        }
    }

    public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
        copy(input, output, new byte[bufferSize]);
    }

    public static void copy(InputStream input, OutputStream output) throws IOException {
        copy(input, output, 4096);
    }

    public static void copy(File input, File output) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(input);
            fos = new FileOutputStream(output);

            copy(fis, fos);
        } finally {
            close(fis);
            close(fos);
        }
    }

    /**
     * No exception <code>InputStream</code> close method.
     */
    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>OutputStream</code> close method.
     */
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>java.io.Reader</code> close method.
     */
    public static void close(Reader rd) {
        if (rd != null) {
            try {
                rd.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>java.io.Writer</code> close method.
     */
    public static void close(Writer wr) {
        if (wr != null) {
            try {
                wr.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }
}

Related

  1. zipInternal(String path, File file, ZipOutputStream os, boolean justFolders, boolean skipManifest)
  2. zipIt(ZipOutputStream zos, String[] directoriesAndFiles, CRC32 crc)
  3. zipOneFile(File file, String base, ZipOutputStream zout)
  4. zipOneFile(File file, ZipOutputStream out, String relativePath)
  5. zipOneFile(String prefix, File file, ZipOutputStream zos)
  6. zipRecursively(String path, File dir, ZipOutputStream out)
  7. zipStoreBuffer(ZipOutputStream zip, String name, byte[] dataBuffer)
  8. zipToStream(File file, int prefixLength, ZipOutputStream out)