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

📄 zip.java

📁 java ant的源码!非常值得看的源码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * @since Ant 1.6.3     */    public void setComment(String comment) {        this.comment = comment;    }    /**     * Comment of the archive     *     * @return Comment of the archive.     * @since Ant 1.6.3     */    public String getComment() {        return comment;    }    /**     * Set the compression level to use.  Default is     * ZipOutputStream.DEFAULT_COMPRESSION.     * @param level compression level.     * @since Ant 1.7     */    public void setLevel(int level) {        this.level = level;    }    /**     * Get the compression level.     * @return compression level.     * @since Ant 1.7     */    public int getLevel() {        return level;    }    /**     * Whether the file modification times will be rounded up to the     * next even number of seconds.     *     * <p>Zip archives store file modification times with a     * granularity of two seconds, so the times will either be rounded     * up or down.  If you round down, the archive will always seem     * out-of-date when you rerun the task, so the default is to round     * up.  Rounding up may lead to a different type of problems like     * JSPs inside a web archive that seem to be slightly more recent     * than precompiled pages, rendering precompilation useless.</p>     * @param r a <code>boolean</code> value     * @since Ant 1.6.2     */    public void setRoundUp(boolean r) {        roundUp = r;    }    /**     * validate and build     * @throws BuildException on error     */    public void execute() throws BuildException {        if (doubleFilePass) {            skipWriting = true;            executeMain();            skipWriting = false;            executeMain();        } else {            executeMain();        }    }    /**     * Build the zip file.     * This is called twice if doubleFilePass is true.     * @throws BuildException on error     */    public void executeMain() throws BuildException {        if (baseDir == null && resources.size() == 0            && groupfilesets.size() == 0 && "zip".equals(archiveType)) {            throw new BuildException("basedir attribute must be set, "                                     + "or at least one "                                     + "resource collection must be given!");        }        if (zipFile == null) {            throw new BuildException("You must specify the "                                     + archiveType + " file to create!");        }        if (zipFile.exists() && !zipFile.isFile()) {            throw new BuildException(zipFile + " is not a file.");        }        if (zipFile.exists() && !zipFile.canWrite()) {            throw new BuildException(zipFile + " is read-only.");        }        // Renamed version of original file, if it exists        File renamedFile = null;        addingNewFiles = true;        // Whether or not an actual update is required -        // we don't need to update if the original file doesn't exist        if (doUpdate && !zipFile.exists()) {            doUpdate = false;            log("ignoring update attribute as " + archiveType                + " doesn't exist.", Project.MSG_DEBUG);        }        // Add the files found in groupfileset to fileset        for (int i = 0; i < groupfilesets.size(); i++) {            log("Processing groupfileset ", Project.MSG_VERBOSE);            FileSet fs = (FileSet) groupfilesets.elementAt(i);            FileScanner scanner = fs.getDirectoryScanner(getProject());            String[] files = scanner.getIncludedFiles();            File basedir = scanner.getBasedir();            for (int j = 0; j < files.length; j++) {                log("Adding file " + files[j] + " to fileset",                    Project.MSG_VERBOSE);                ZipFileSet zf = new ZipFileSet();                zf.setProject(getProject());                zf.setSrc(new File(basedir, files[j]));                add(zf);                filesetsFromGroupfilesets.addElement(zf);            }        }        // collect filesets to pass them to getResourcesToAdd        Vector vfss = new Vector();        if (baseDir != null) {            FileSet fs = (FileSet) getImplicitFileSet().clone();            fs.setDir(baseDir);            vfss.addElement(fs);        }        for (int i = 0; i < resources.size(); i++) {            ResourceCollection rc = (ResourceCollection) resources.elementAt(i);            vfss.addElement(rc);        }        ResourceCollection[] fss = new ResourceCollection[vfss.size()];        vfss.copyInto(fss);        boolean success = false;        try {            // can also handle empty archives            ArchiveState state = getResourcesToAdd(fss, zipFile, false);            // quick exit if the target is up to date            if (!state.isOutOfDate()) {                return;            }            if (!zipFile.exists() && state.isWithoutAnyResources()) {                createEmptyZip(zipFile);                return;            }            Resource[][] addThem = state.getResourcesToAdd();            if (doUpdate) {                renamedFile =                    FILE_UTILS.createTempFile("zip", ".tmp",                                              zipFile.getParentFile());                renamedFile.deleteOnExit();                try {                    FILE_UTILS.rename(zipFile, renamedFile);                } catch (SecurityException e) {                    throw new BuildException(                        "Not allowed to rename old file ("                        + zipFile.getAbsolutePath()                        + ") to temporary file");                } catch (IOException e) {                    throw new BuildException(                        "Unable to rename old file ("                        + zipFile.getAbsolutePath()                        + ") to temporary file");                }            }            String action = doUpdate ? "Updating " : "Building ";            log(action + archiveType + ": " + zipFile.getAbsolutePath());            ZipOutputStream zOut = null;            try {                if (!skipWriting) {                    zOut = new ZipOutputStream(zipFile);                    zOut.setEncoding(encoding);                    zOut.setMethod(doCompress                        ? ZipOutputStream.DEFLATED : ZipOutputStream.STORED);                    zOut.setLevel(level);                }                initZipOutputStream(zOut);                // Add the explicit resource collections to the archive.                for (int i = 0; i < fss.length; i++) {                    if (addThem[i].length != 0) {                        addResources(fss[i], addThem[i], zOut);                    }                }                if (doUpdate) {                    addingNewFiles = false;                    ZipFileSet oldFiles = new ZipFileSet();                    oldFiles.setProject(getProject());                    oldFiles.setSrc(renamedFile);                    oldFiles.setDefaultexcludes(false);                    for (int i = 0; i < addedFiles.size(); i++) {                        PatternSet.NameEntry ne = oldFiles.createExclude();                        ne.setName((String) addedFiles.elementAt(i));                    }                    DirectoryScanner ds =                        oldFiles.getDirectoryScanner(getProject());                    ((ZipScanner) ds).setEncoding(encoding);                    String[] f = ds.getIncludedFiles();                    Resource[] r = new Resource[f.length];                    for (int i = 0; i < f.length; i++) {                        r[i] = ds.getResource(f[i]);                    }                    if (!doFilesonly) {                        String[] d = ds.getIncludedDirectories();                        Resource[] dr = new Resource[d.length];                        for (int i = 0; i < d.length; i++) {                            dr[i] = ds.getResource(d[i]);                        }                        Resource[] tmp = r;                        r = new Resource[tmp.length + dr.length];                        System.arraycopy(dr, 0, r, 0, dr.length);                        System.arraycopy(tmp, 0, r, dr.length, tmp.length);                    }                    addResources(oldFiles, r, zOut);                }                if (zOut != null) {                    zOut.setComment(comment);                }                finalizeZipOutputStream(zOut);                // If we've been successful on an update, delete the                // temporary file                if (doUpdate) {                    if (!renamedFile.delete()) {                        log ("Warning: unable to delete temporary file "                            + renamedFile.getName(), Project.MSG_WARN);                    }                }                success = true;            } finally {                // Close the output stream.                try {                    if (zOut != null) {                        zOut.close();                    }                } catch (IOException ex) {                    // If we're in this finally clause because of an                    // exception, we don't really care if there's an                    // exception when closing the stream. E.g. if it                    // throws "ZIP file must have at least one entry",                    // because an exception happened before we added                    // any files, then we must swallow this                    // exception. Otherwise, the error that's reported                    // will be the close() error, which is not the                    // real cause of the problem.                    if (success) {                        throw ex;                    }                }            }        } catch (IOException ioe) {            String msg = "Problem creating " + archiveType + ": "                + ioe.getMessage();            // delete a bogus ZIP file (but only if it's not the original one)            if ((!doUpdate || renamedFile != null) && !zipFile.delete()) {                msg += " (and the archive is probably corrupt but I could not "                    + "delete it)";            }            if (doUpdate && renamedFile != null) {                try {                    FILE_UTILS.rename(renamedFile, zipFile);                } catch (IOException e) {                    msg += " (and I couldn't rename the temporary file "                            + renamedFile.getName() + " back)";                }            }            throw new BuildException(msg, ioe, getLocation());        } finally {            cleanUp();        }    }    /**     * Indicates if the task is adding new files into the archive as opposed to     * copying back unchanged files from the backup copy     * @return true if adding new files     */    protected final boolean isAddingNewFiles() {        return addingNewFiles;    }    /**     * Add the given resources.     *     * @param fileset may give additional information like fullpath or     * permissions.     * @param resources the resources to add     * @param zOut the stream to write to     * @throws IOException on error     *     * @since Ant 1.5.2     */    protected final void addResources(FileSet fileset, Resource[] resources,                                      ZipOutputStream zOut)        throws IOException {        String prefix = "";        String fullpath = "";        int dirMode = ArchiveFileSet.DEFAULT_DIR_MODE;        int fileMode = ArchiveFileSet.DEFAULT_FILE_MODE;        ArchiveFileSet zfs = null;        if (fileset instanceof ArchiveFileSet) {            zfs = (ArchiveFileSet) fileset;            prefix = zfs.getPrefix(getProject());            fullpath = zfs.getFullpath(getProject());            dirMode = zfs.getDirMode(getProject());            fileMode = zfs.getFileMode(getProject());        }        if (prefix.length() > 0 && fullpath.length() > 0) {            throw new BuildException("Both prefix and fullpath attributes must"                                     + " not be set on the same fileset.");        }        if (resources.length != 1 && fullpath.length() > 0) {            throw new BuildException("fullpath attribute may only be specified"                                     + " for filesets that specify a single"                                     + " file.");        }

⌨️ 快捷键说明

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