Here you can find the source of zipDirectory(File dir, File zipFile)
Parameter | Description |
---|---|
dir | the directory to zip. |
zipFile | the output zip file. |
public static synchronized boolean zipDirectory(File dir, File zipFile)
//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.FileOutputStream; 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 { /**/*ww w .j a v a 2 s .com*/ * Zip a directory and its subdirectories, preserving the name of * the root directory (dir) in all paths in the zip file. * @param dir the directory to zip. * @param zipFile the output zip file. * @return true if the operation succeeded completely; false otherwise. */ public static synchronized boolean zipDirectory(File dir, File zipFile) { return zipDirectory(dir, zipFile, false); } /** * Zip a directory and its subdirectories. * @param dir the directory to zip. * @param zipFile the output zip file. * @param suppressRoot true if the path to the root directory (dir) is * to be suppressed; false if all paths in the zip file are to start * with the name of the root directory. * @return true if the operation succeeded completely; false otherwise. */ public static synchronized boolean zipDirectory(File dir, File zipFile, boolean suppressRoot) { try { dir = dir.getCanonicalFile(); int rootLength; if (suppressRoot) { //Get the length of the dir path rootLength = dir.getAbsolutePath().length(); } else { //Get the length of the parent path File parent = dir.getParentFile(); rootLength = parent.getAbsolutePath().length(); } rootLength++; //allow for the slash that will appear in files that are zipped //Get the streams FileOutputStream fout = new FileOutputStream(zipFile); ZipOutputStream zout = new ZipOutputStream(fout); zipDirectory(zout, dir, rootLength); zout.close(); return true; } catch (Exception ex) { return false; } } private static synchronized void zipDirectory(ZipOutputStream zout, File dir, int rootLength) throws Exception { if (dir.isDirectory()) { String name = dir.getAbsolutePath() + "/"; if (name.length() > rootLength) { name = name.substring(rootLength); if (!name.endsWith("/")) name += "/"; ZipEntry ze = new ZipEntry(name); zout.putNextEntry(ze); } File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) zipDirectory(zout, file, rootLength); else zipFile(zout, file, rootLength); } } } /** * 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) { } } } private static synchronized void zipFile(ZipOutputStream zout, File file, int rootLength) throws Exception { FileInputStream fin; 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(); } } }