📄 tar.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.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Vector;import java.util.zip.GZIPOutputStream;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.DirectoryScanner;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.Resource;import org.apache.tools.ant.types.ResourceCollection;import org.apache.tools.ant.types.resources.ArchiveResource;import org.apache.tools.ant.types.resources.FileResource;import org.apache.tools.ant.types.selectors.SelectorUtils;import org.apache.tools.ant.types.resources.TarResource;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.MergingMapper;import org.apache.tools.ant.util.SourceFileScanner;import org.apache.tools.bzip2.CBZip2OutputStream;import org.apache.tools.tar.TarConstants;import org.apache.tools.tar.TarEntry;import org.apache.tools.tar.TarOutputStream;/** * Creates a tar archive. * * @since Ant 1.1 * * @ant.task category="packaging" */public class Tar extends MatchingTask { private static final int BUFFER_SIZE = 8 * 1024; /** * @deprecated since 1.5.x. * Tar.WARN is deprecated and is replaced with * Tar.TarLongFileMode.WARN */ public static final String WARN = "warn"; /** * @deprecated since 1.5.x. * Tar.FAIL is deprecated and is replaced with * Tar.TarLongFileMode.FAIL */ public static final String FAIL = "fail"; /** * @deprecated since 1.5.x. * Tar.TRUNCATE is deprecated and is replaced with * Tar.TarLongFileMode.TRUNCATE */ public static final String TRUNCATE = "truncate"; /** * @deprecated since 1.5.x. * Tar.GNU is deprecated and is replaced with * Tar.TarLongFileMode.GNU */ public static final String GNU = "gnu"; /** * @deprecated since 1.5.x. * Tar.OMIT is deprecated and is replaced with * Tar.TarLongFileMode.OMIT */ public static final String OMIT = "omit"; // CheckStyle:VisibilityModifier OFF - bc File tarFile; File baseDir; private TarLongFileMode longFileMode = new TarLongFileMode(); // need to keep the package private version for backwards compatibility Vector filesets = new Vector(); // we must keep two lists since other classes may modify the // filesets Vector (it is package private) without us noticing private Vector resourceCollections = new Vector(); Vector fileSetFiles = new Vector(); // CheckStyle:VisibilityModifier ON /** * Indicates whether the user has been warned about long files already. */ private boolean longWarningGiven = false; private TarCompressionMethod compression = new TarCompressionMethod(); /** * Add a new fileset with the option to specify permissions * @return the tar fileset to be used as the nested element. */ public TarFileSet createTarFileSet() { TarFileSet fs = new TarFileSet(); fs.setProject(getProject()); filesets.addElement(fs); return fs; } /** * Add a collection of resources to archive. * @param res a resource collection to archive. * @since Ant 1.7 */ public void add(ResourceCollection res) { resourceCollections.add(res); } /** * Set is the name/location of where to create the tar file. * @param tarFile the location of the tar file. * @deprecated since 1.5.x. * For consistency with other tasks, please use setDestFile(). */ public void setTarfile(File tarFile) { this.tarFile = tarFile; } /** * Set is the name/location of where to create the tar file. * @since Ant 1.5 * @param destFile The output of the tar */ public void setDestFile(File destFile) { this.tarFile = destFile; } /** * This is the base directory to look in for things to tar. * @param baseDir the base directory. */ public void setBasedir(File baseDir) { this.baseDir = baseDir; } /** * Set how to handle long files, those with a path>100 chars. * Optional, default=warn. * <p> * Allowable values are * <ul> * <li> truncate - paths are truncated to the maximum length * <li> fail - paths greater than the maximum cause a build exception * <li> warn - paths greater than the maximum cause a warning and GNU is used * <li> gnu - GNU extensions are used for any paths greater than the maximum. * <li> omit - paths greater than the maximum are omitted from the archive * </ul> * @param mode the mode string to handle long files. * @deprecated since 1.5.x. * setLongFile(String) is deprecated and is replaced with * setLongFile(Tar.TarLongFileMode) to make Ant's Introspection * mechanism do the work and also to encapsulate operations on * the mode in its own class. */ public void setLongfile(String mode) { log("DEPRECATED - The setLongfile(String) method has been deprecated." + " Use setLongfile(Tar.TarLongFileMode) instead."); this.longFileMode = new TarLongFileMode(); longFileMode.setValue(mode); } /** * Set how to handle long files, those with a path>100 chars. * Optional, default=warn. * <p> * Allowable values are * <ul> * <li> truncate - paths are truncated to the maximum length * <li> fail - paths greater than the maximum cause a build exception * <li> warn - paths greater than the maximum cause a warning and GNU is used * <li> gnu - GNU extensions are used for any paths greater than the maximum. * <li> omit - paths greater than the maximum are omitted from the archive * </ul> * @param mode the mode to handle long file names. */ public void setLongfile(TarLongFileMode mode) { this.longFileMode = mode; } /** * Set compression method. * Allowable values are * <ul> * <li> none - no compression * <li> gzip - Gzip compression * <li> bzip2 - Bzip2 compression * </ul> * @param mode the compression method. */ public void setCompression(TarCompressionMethod mode) { this.compression = mode; } /** * do the business * @throws BuildException on error */ public void execute() throws BuildException { if (tarFile == null) { throw new BuildException("tarfile attribute must be set!", getLocation()); } if (tarFile.exists() && tarFile.isDirectory()) { throw new BuildException("tarfile is a directory!", getLocation()); } if (tarFile.exists() && !tarFile.canWrite()) { throw new BuildException("Can not write to the specified tarfile!", getLocation()); } Vector savedFileSets = (Vector) filesets.clone(); try { if (baseDir != null) { if (!baseDir.exists()) { throw new BuildException("basedir does not exist!", getLocation()); } // add the main fileset to the list of filesets to process. TarFileSet mainFileSet = new TarFileSet(fileset); mainFileSet.setDir(baseDir); filesets.addElement(mainFileSet); } if (filesets.size() == 0 && resourceCollections.size() == 0) { throw new BuildException("You must supply either a basedir " + "attribute or some nested resource" + " collections.", getLocation()); } // check if tar is out of date with respect to each // fileset boolean upToDate = true; for (Enumeration e = filesets.elements(); e.hasMoreElements();) { upToDate &= check((TarFileSet) e.nextElement()); } for (Enumeration e = resourceCollections.elements(); e.hasMoreElements();) { upToDate &= check((ResourceCollection) e.nextElement()); } if (upToDate) { log("Nothing to do: " + tarFile.getAbsolutePath() + " is up to date.", Project.MSG_INFO); return; } log("Building tar: " + tarFile.getAbsolutePath(), Project.MSG_INFO); TarOutputStream tOut = null; try { tOut = new TarOutputStream( compression.compress( new BufferedOutputStream( new FileOutputStream(tarFile)))); tOut.setDebug(true); if (longFileMode.isTruncateMode()) { tOut.setLongFileMode(TarOutputStream.LONGFILE_TRUNCATE); } else if (longFileMode.isFailMode() || longFileMode.isOmitMode()) { tOut.setLongFileMode(TarOutputStream.LONGFILE_ERROR); } else { // warn or GNU tOut.setLongFileMode(TarOutputStream.LONGFILE_GNU); } longWarningGiven = false; for (Enumeration e = filesets.elements(); e.hasMoreElements();) { tar((TarFileSet) e.nextElement(), tOut); } for (Enumeration e = resourceCollections.elements(); e.hasMoreElements();) { tar((ResourceCollection) e.nextElement(), tOut); } } catch (IOException ioe) { String msg = "Problem creating TAR: " + ioe.getMessage(); throw new BuildException(msg, ioe, getLocation()); } finally { FileUtils.close(tOut); } } finally { filesets = savedFileSets; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -