⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tarentry.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  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. * *//* * This package is based on the work done by Timothy Gerard Endres * (time@ice.com) to whom the Ant project is very grateful for his great code. */package org.apache.tools.tar;import java.io.File;import java.util.Date;import java.util.Locale;/** * This class represents an entry in a Tar archive. It consists * of the entry's header, as well as the entry's File. Entries * can be instantiated in one of three ways, depending on how * they are to be used. * <p> * TarEntries that are created from the header bytes read from * an archive are instantiated with the TarEntry( byte[] ) * constructor. These entries will be used when extracting from * or listing the contents of an archive. These entries have their * header filled in using the header bytes. They also set the File * to null, since they reference an archive entry not a file. * <p> * TarEntries that are created from Files that are to be written * into an archive are instantiated with the TarEntry( File ) * constructor. These entries have their header filled in using * the File's information. They also keep a reference to the File * for convenience when writing entries. * <p> * Finally, TarEntries can be constructed from nothing but a name. * This allows the programmer to construct the entry by hand, for * instance when only an InputStream is available for writing to * the archive, and the header information is constructed from * other information. In this case the header fields are set to * defaults and the File is set to null. * * <p> * The C structure for a Tar Entry's header is: * <pre> * struct header { * char name[NAMSIZ]; * char mode[8]; * char uid[8]; * char gid[8]; * char size[12]; * char mtime[12]; * char chksum[8]; * char linkflag; * char linkname[NAMSIZ]; * char magic[8]; * char uname[TUNMLEN]; * char gname[TGNMLEN]; * char devmajor[8]; * char devminor[8]; * } header; * </pre> * */public class TarEntry implements TarConstants {    /** The entry's name. */    private StringBuffer name;    /** The entry's permission mode. */    private int mode;    /** The entry's user id. */    private int userId;    /** The entry's group id. */    private int groupId;    /** The entry's size. */    private long size;    /** The entry's modification time. */    private long modTime;    /** The entry's link flag. */    private byte linkFlag;    /** The entry's link name. */    private StringBuffer linkName;    /** The entry's magic tag. */    private StringBuffer magic;    /** The entry's user name. */    private StringBuffer userName;    /** The entry's group name. */    private StringBuffer groupName;    /** The entry's major device number. */    private int devMajor;    /** The entry's minor device number. */    private int devMinor;    /** The entry's file reference */    private File file;    /** Maximum length of a user's name in the tar file */    public static final int MAX_NAMELEN = 31;    /** Default permissions bits for directories */    public static final int DEFAULT_DIR_MODE = 040755;    /** Default permissions bits for files */    public static final int DEFAULT_FILE_MODE = 0100644;    /** Convert millis to seconds */    public static final int MILLIS_PER_SECOND = 1000;    /**     * Construct an empty entry and prepares the header values.     */    private TarEntry () {        this.magic = new StringBuffer(TMAGIC);        this.name = new StringBuffer();        this.linkName = new StringBuffer();        String user = System.getProperty("user.name", "");        if (user.length() > MAX_NAMELEN) {            user = user.substring(0, MAX_NAMELEN);        }        this.userId = 0;        this.groupId = 0;        this.userName = new StringBuffer(user);        this.groupName = new StringBuffer("");        this.file = null;    }    /**     * Construct an entry with only a name. This allows the programmer     * to construct the entry's header "by hand". File is set to null.     *     * @param name the entry name     */    public TarEntry(String name) {        this();        boolean isDir = name.endsWith("/");        this.devMajor = 0;        this.devMinor = 0;        this.name = new StringBuffer(name);        this.mode = isDir ? DEFAULT_DIR_MODE : DEFAULT_FILE_MODE;        this.linkFlag = isDir ? LF_DIR : LF_NORMAL;        this.userId = 0;        this.groupId = 0;        this.size = 0;        this.modTime = (new Date()).getTime() / MILLIS_PER_SECOND;        this.linkName = new StringBuffer("");        this.userName = new StringBuffer("");        this.groupName = new StringBuffer("");        this.devMajor = 0;        this.devMinor = 0;    }    /**     * Construct an entry with a name an a link flag.     *     * @param name the entry name     * @param linkFlag the entry link flag.     */    public TarEntry(String name, byte linkFlag) {        this(name);        this.linkFlag = linkFlag;    }    /**     * Construct an entry for a file. File is set to file, and the     * header is constructed from information from the file.     *     * @param file The file that the entry represents.     */    public TarEntry(File file) {        this();        this.file = file;        String fileName = file.getPath();        String osname = System.getProperty("os.name").toLowerCase(Locale.US);        if (osname != null) {            // Strip off drive letters!            // REVIEW Would a better check be "(File.separator == '\')"?            if (osname.startsWith("windows")) {                if (fileName.length() > 2) {                    char ch1 = fileName.charAt(0);                    char ch2 = fileName.charAt(1);                    if (ch2 == ':'                            && ((ch1 >= 'a' && ch1 <= 'z')                                || (ch1 >= 'A' && ch1 <= 'Z'))) {                        fileName = fileName.substring(2);                    }                }            } else if (osname.indexOf("netware") > -1) {                int colon = fileName.indexOf(':');                if (colon != -1) {                    fileName = fileName.substring(colon + 1);                }            }        }        fileName = fileName.replace(File.separatorChar, '/');        // No absolute pathnames        // Windows (and Posix?) paths can start with "\\NetworkDrive\",        // so we loop on starting /'s.        while (fileName.startsWith("/")) {            fileName = fileName.substring(1);        }        this.linkName = new StringBuffer("");        this.name = new StringBuffer(fileName);        if (file.isDirectory()) {            this.mode = DEFAULT_DIR_MODE;            this.linkFlag = LF_DIR;            if (this.name.charAt(this.name.length() - 1) != '/') {                this.name.append("/");            }        } else {            this.mode = DEFAULT_FILE_MODE;            this.linkFlag = LF_NORMAL;        }        this.size = file.length();        this.modTime = file.lastModified() / MILLIS_PER_SECOND;        this.devMajor = 0;        this.devMinor = 0;    }    /**     * Construct an entry from an archive's header bytes. File is set     * to null.     *     * @param headerBuf The header bytes from a tar archive entry.     */    public TarEntry(byte[] headerBuf) {        this();        this.parseTarHeader(headerBuf);    }    /**     * Determine if the two entries are equal. Equality is determined     * by the header names being equal.     *     * @param it Entry to be checked for equality.     * @return True if the entries are equal.     */    public boolean equals(TarEntry it) {        return this.getName().equals(it.getName());    }    /**     * Determine if the two entries are equal. Equality is determined     * by the header names being equal.     *     * @param it Entry to be checked for equality.     * @return True if the entries are equal.     */    public boolean equals(Object it) {        if (it == null || getClass() != it.getClass()) {            return false;        }        return equals((TarEntry) it);    }    /**     * Hashcodes are based on entry names.     *     * @return the entry hashcode     */    public int hashCode() {        return getName().hashCode();    }    /**     * Determine if the given entry is a descendant of this entry.     * Descendancy is determined by the name of the descendant     * starting with this entry's name.     *     * @param desc Entry to be checked as a descendent of this.     * @return True if entry is a descendant of this.     */    public boolean isDescendent(TarEntry desc) {        return desc.getName().startsWith(this.getName());    }    /**     * Get this entry's name.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -