Here you can find the source of zipDirectory(File directory, File zipFile, Pattern exclusion)
Parameter | Description |
---|---|
exclusion | can be null |
public static void zipDirectory(File directory, File zipFile, Pattern exclusion)
//package com.java2s; /*//from w w w . java 2s. c om * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. * * Copyright (C) 2006-2010 Adele Team/LIG/Grenoble University, France */ import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static final int BUFFER_SIZE = 2048; /** * Utility method to zip a directory hierarchy recursively into a single * compressed file. * * @param exclusion * can be null * */ public static void zipDirectory(File directory, File zipFile, Pattern exclusion) { zipDirectory(directory, zipFile, new byte[BUFFER_SIZE], exclusion); } /** * Utility method to zip a directory hierarchy recursively into a single * compressed file. * * This variant allows the caller to speciy the buffer to be used, so that * the same buffer can be reused for different calls. * * @param exclusion * can be null */ public static void zipDirectory(File directory, File zipFile, byte buffer[], Pattern exclusion) { try { zipFile.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(zipFile); ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(out)); zipDirectory(directory, zip, buffer, exclusion); zip.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } /** * TODO: rewrite doc Writes an entry in the ZipFile, If the entry is a * directory it adds the entries recursively. Entries are named relative to * the base directory specified. * * A single data buffer is used to avoid recursive creation of buffer * objects, so concurrent execution of this method is disallowed using the * same buffer. * * @param exclusion * can be null */ public static void zipDirectory(File base, ZipOutputStream zip, byte buffer[], Pattern exclusion) { List stack = new ArrayList(); String baseName = base.getAbsolutePath(); ZipEntry zipEntry; String entryName; File entry = base; int i = 0; stack.add(new File[] { base.getAbsoluteFile() }); synchronized (buffer) { while (stack.size() > i) { File[] files = (File[]) stack.get(i); stack.set(i, null); i++; for (int j = 0; j < files.length; j++) { try { entry = files[j]; entryName = entry.getAbsolutePath(); if (entryName.length() == baseName.length()) { entryName = ""; } else { entryName = entryName.substring(baseName.length() + 1); } entryName = entryName.replace(File.separatorChar, '/'); if (entry.isDirectory()) { entryName = entryName + "/"; } if ((exclusion != null) && exclusion.matcher(entryName).matches()) { continue; } zipEntry = new ZipEntry(entryName); zipEntry.setTime(entry.lastModified()); zip.putNextEntry(zipEntry); if (entry.isDirectory()) { /* * Add a new directory entry * to the zip file */ /* * Recursively transverse the hierarchy (deep * transversal) */ stack.add(entry.listFiles()); } else { /* Add a new entry to the zip file */ FileInputStream file = new FileInputStream(entry); int count; while ((count = file.read(buffer, 0, buffer.length)) != -1) { zip.write(buffer, 0, count); } file.close(); } zip.closeEntry(); } catch (Exception io) { System.out.println("Add Entry " + base.getAbsolutePath() + " " + entry.getAbsolutePath()); io.printStackTrace(); } } } } } }