Java ZipOutputStream Write zipFile(ZipOutputStream zout, File file, int rootLength)

Here you can find the source of zipFile(ZipOutputStream zout, File file, int rootLength)

Description

zip File

License

Open Source License

Declaration

private static synchronized void zipFile(ZipOutputStream zout, File file, int rootLength) throws Exception 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class Main {
    private static synchronized void zipFile(ZipOutputStream zout, File file, int rootLength) throws Exception {
        FileInputStream fin;/*from   www .  j a  v a 2  s  . c o  m*/
        ZipEntry ze;
        byte[] buffer = new byte[10000];
        int bytesread;
        String entryname = file.getAbsolutePath().substring(rootLength);
        entryname = entryname.replaceAll("\\\\", "/");
        ze = new ZipEntry(entryname);
        if (file.exists()) {
            fin = new FileInputStream(file);
            zout.putNextEntry(ze);
            while ((bytesread = fin.read(buffer)) > 0)
                zout.write(buffer, 0, bytesread);
            zout.closeEntry();
            fin.close();
        }
    }

    /**
     * Close an InputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(InputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close an OutputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(OutputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Writer and ignore Exceptions.
     * @param writer the writer to close.
     */
    public static void close(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Reader and ignore Exceptions.
     * @param reader the reader to close.
     */
    public static void close(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a JarFile and ignore Exceptions.
     * @param jarFile the JarFile to close.
     */
    public static void close(JarFile jarFile) {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a ZipFile and ignore Exceptions.
     * @param zipFile the zipFile to close.
     */
    public static void close(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (Exception ignore) {
            }
        }
    }
}

Related

  1. zipFile(ZipOutputStream out, File sourceFile)
  2. zipFile(ZipOutputStream out, String stripPath, File file, char pathSeparator)
  3. zipFile(ZipOutputStream zipOut, String path, File file)
  4. zipFile(ZipOutputStream zos, BufferedOutputStream out, File destPath, String originalPath)
  5. zipFile(ZipOutputStream zos, File file, String requestName, byte[] buf)
  6. zipInternal(String path, File file, ZipOutputStream os)
  7. zipInternal(String path, File file, ZipOutputStream os, boolean justFolders, boolean skipManifest)
  8. zipIt(ZipOutputStream zos, String[] directoriesAndFiles, CRC32 crc)
  9. zipOneFile(File file, String base, ZipOutputStream zout)