📄 zip.java
字号:
/* * 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. * */package org.apache.tools.ant.taskdefs;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;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.util.ArrayList;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Stack;import java.util.Vector;import java.util.zip.CRC32;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.DirectoryScanner;import org.apache.tools.ant.FileScanner;import org.apache.tools.ant.Project;import org.apache.tools.ant.types.ArchiveFileSet;import org.apache.tools.ant.types.EnumeratedAttribute;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.types.PatternSet;import org.apache.tools.ant.types.Resource;import org.apache.tools.ant.types.ResourceCollection;import org.apache.tools.ant.types.ZipFileSet;import org.apache.tools.ant.types.ZipScanner;import org.apache.tools.ant.types.resources.ArchiveResource;import org.apache.tools.ant.types.resources.FileResource;import org.apache.tools.ant.util.FileNameMapper;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.GlobPatternMapper;import org.apache.tools.ant.util.IdentityMapper;import org.apache.tools.ant.util.MergingMapper;import org.apache.tools.ant.util.ResourceUtils;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipExtraField;import org.apache.tools.zip.ZipFile;import org.apache.tools.zip.ZipOutputStream;/** * Create a Zip file. * * @since Ant 1.1 * * @ant.task category="packaging" */public class Zip extends MatchingTask { private static final int BUFFER_SIZE = 8 * 1024; private static final int ROUNDUP_MILLIS = 1999; // 2 seconds - 1 // CheckStyle:VisibilityModifier OFF - bc protected File zipFile; // use to scan own archive private ZipScanner zs; private File baseDir; protected Hashtable entries = new Hashtable(); private Vector groupfilesets = new Vector(); private Vector filesetsFromGroupfilesets = new Vector(); protected String duplicate = "add"; private boolean doCompress = true; private boolean doUpdate = false; // shadow of the above if the value is altered in execute private boolean savedDoUpdate = false; private boolean doFilesonly = false; protected String archiveType = "zip"; // For directories: private static final long EMPTY_CRC = new CRC32 ().getValue (); protected String emptyBehavior = "skip"; private Vector resources = new Vector(); protected Hashtable addedDirs = new Hashtable(); private Vector addedFiles = new Vector(); protected boolean doubleFilePass = false; protected boolean skipWriting = false; private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); // CheckStyle:VisibilityModifier ON // This boolean is set if the task detects that the // target is outofdate and has written to the target file. private boolean updatedFile = false; /** * true when we are adding new files into the Zip file, as opposed * to adding back the unchanged files */ private boolean addingNewFiles = false; /** * Encoding to use for filenames, defaults to the platform's * default encoding. */ private String encoding; /** * Whether the original compression of entries coming from a ZIP * archive should be kept (for example when updating an archive). * * @since Ant 1.6 */ private boolean keepCompression = false; /** * Whether the file modification times will be rounded up to the * next even number of seconds. * * @since Ant 1.6.2 */ private boolean roundUp = true; /** * Comment for the archive. * @since Ant 1.6.3 */ private String comment = ""; private int level = ZipOutputStream.DEFAULT_COMPRESSION; /** * This is the name/location of where to * create the .zip file. * @param zipFile the path of the zipFile * @deprecated since 1.5.x. * Use setDestFile(File) instead. * @ant.attribute ignore="true" */ public void setZipfile(File zipFile) { setDestFile(zipFile); } /** * This is the name/location of where to * create the file. * @param file the path of the zipFile * @since Ant 1.5 * @deprecated since 1.5.x. * Use setDestFile(File) instead. * @ant.attribute ignore="true" */ public void setFile(File file) { setDestFile(file); } /** * The file to create; required. * @since Ant 1.5 * @param destFile The new destination File */ public void setDestFile(File destFile) { this.zipFile = destFile; } /** * The file to create. * @return the destination file * @since Ant 1.5.2 */ public File getDestFile() { return zipFile; } /** * Directory from which to archive files; optional. * @param baseDir the base directory */ public void setBasedir(File baseDir) { this.baseDir = baseDir; } /** * Whether we want to compress the files or only store them; * optional, default=true; * @param c if true, compress the files */ public void setCompress(boolean c) { doCompress = c; } /** * Whether we want to compress the files or only store them; * @return true if the files are to be compressed * @since Ant 1.5.2 */ public boolean isCompress() { return doCompress; } /** * If true, emulate Sun's jar utility by not adding parent directories; * optional, defaults to false. * @param f if true, emulate sun's jar by not adding parent directories */ public void setFilesonly(boolean f) { doFilesonly = f; } /** * If true, updates an existing file, otherwise overwrite * any existing one; optional defaults to false. * @param c if true, updates an existing zip file */ public void setUpdate(boolean c) { doUpdate = c; savedDoUpdate = c; } /** * Are we updating an existing archive? * @return true if updating an existing archive */ public boolean isInUpdateMode() { return doUpdate; } /** * Adds a set of files. * @param set the fileset to add */ public void addFileset(FileSet set) { add(set); } /** * Adds a set of files that can be * read from an archive and be given a prefix/fullpath. * @param set the zipfileset to add */ public void addZipfileset(ZipFileSet set) { add(set); } /** * Add a collection of resources to be archived. * @param a the resources to archive * @since Ant 1.7 */ public void add(ResourceCollection a) { resources.add(a); } /** * Adds a group of zip files. * @param set the group (a fileset) to add */ public void addZipGroupFileset(FileSet set) { groupfilesets.addElement(set); } /** * Sets behavior for when a duplicate file is about to be added - * one of <code>add</code>, <code>preserve</code> or <code>fail</code>. * Possible values are: <code>add</code> (keep both * of the files); <code>preserve</code> (keep the first version * of the file found); <code>fail</code> halt a problem * Default for zip tasks is <code>add</code> * @param df a <code>Duplicate</code> enumerated value */ public void setDuplicate(Duplicate df) { duplicate = df.getValue(); } /** * Possible behaviors when there are no matching files for the task: * "fail", "skip", or "create". */ public static class WhenEmpty extends EnumeratedAttribute { /** * The string values for the enumerated value * @return the values */ public String[] getValues() { return new String[] {"fail", "skip", "create"}; } } /** * Sets behavior of the task when no files match. * Possible values are: <code>fail</code> (throw an exception * and halt the build); <code>skip</code> (do not create * any archive, but issue a warning); <code>create</code> * (make an archive with no entries). * Default for zip tasks is <code>skip</code>; * for jar tasks, <code>create</code>. * @param we a <code>WhenEmpty</code> enumerated value */ public void setWhenempty(WhenEmpty we) { emptyBehavior = we.getValue(); } /** * Encoding to use for filenames, defaults to the platform's * default encoding. * * <p>For a list of possible values see <a * href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html</a>.</p> * @param encoding the encoding name */ public void setEncoding(String encoding) { this.encoding = encoding; } /** * Encoding to use for filenames. * @return the name of the encoding to use * @since Ant 1.5.2 */ public String getEncoding() { return encoding; } /** * Whether the original compression of entries coming from a ZIP * archive should be kept (for example when updating an archive). * Default is false. * @param keep if true, keep the original compression * @since Ant 1.6 */ public void setKeepCompression(boolean keep) { keepCompression = keep; } /** * Comment to use for archive. * * @param comment The content of the comment. * @since Ant 1.6.3 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -