📄 zip.java
字号:
if (dir != null && dir.exists()) { // ZIPs store time with a granularity of 2 seconds, round up ze.setTime(dir.lastModified() + (roundUp ? ROUNDUP_MILLIS : 0)); } else { // ZIPs store time with a granularity of 2 seconds, round up ze.setTime(System.currentTimeMillis() + (roundUp ? ROUNDUP_MILLIS : 0)); } ze.setSize (0); ze.setMethod (ZipEntry.STORED); // This is faintly ridiculous: ze.setCrc (EMPTY_CRC); ze.setUnixMode(mode); if (extra != null) { ze.setExtraFields(extra); } zOut.putNextEntry(ze); } } /** * Adds a new entry to the archive, takes care of duplicates as well. * * @param in the stream to read data for the entry from. * @param zOut the stream to write to. * @param vPath the name this entry shall have in the archive. * @param lastModified last modification time for the entry. * @param fromArchive the original archive we are copying this * entry from, will be null if we are not copying from an archive. * @param mode the Unix permissions to set. * * @since Ant 1.5.2 * @throws IOException on error */ protected void zipFile(InputStream in, ZipOutputStream zOut, String vPath, long lastModified, File fromArchive, int mode) throws IOException { if (entries.contains(vPath)) { if (duplicate.equals("preserve")) { log(vPath + " already added, skipping", Project.MSG_INFO); return; } else if (duplicate.equals("fail")) { throw new BuildException("Duplicate file " + vPath + " was found and the duplicate " + "attribute is 'fail'."); } else { // duplicate equal to add, so we continue log("duplicate file " + vPath + " found, adding.", Project.MSG_VERBOSE); } } else { log("adding entry " + vPath, Project.MSG_VERBOSE); } entries.put(vPath, vPath); if (!skipWriting) { ZipEntry ze = new ZipEntry(vPath); ze.setTime(lastModified); ze.setMethod(doCompress ? ZipEntry.DEFLATED : ZipEntry.STORED); /* * ZipOutputStream.putNextEntry expects the ZipEntry to * know its size and the CRC sum before you start writing * the data when using STORED mode - unless it is seekable. * * This forces us to process the data twice. */ if (!zOut.isSeekable() && !doCompress) { long size = 0; CRC32 cal = new CRC32(); if (!in.markSupported()) { // Store data into a byte[] ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; do { size += count; cal.update(buffer, 0, count); bos.write(buffer, 0, count); count = in.read(buffer, 0, buffer.length); } while (count != -1); in = new ByteArrayInputStream(bos.toByteArray()); } else { in.mark(Integer.MAX_VALUE); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; do { size += count; cal.update(buffer, 0, count); count = in.read(buffer, 0, buffer.length); } while (count != -1); in.reset(); } ze.setSize(size); ze.setCrc(cal.getValue()); } ze.setUnixMode(mode); zOut.putNextEntry(ze); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; do { if (count != 0) { zOut.write(buffer, 0, count); } count = in.read(buffer, 0, buffer.length); } while (count != -1); } addedFiles.addElement(vPath); } /** * Method that gets called when adding from <code>java.io.File</code> instances. * * <p>This implementation delegates to the six-arg version.</p> * * @param file the file to add to the archive * @param zOut the stream to write to * @param vPath the name this entry shall have in the archive * @param mode the Unix permissions to set. * @throws IOException on error * * @since Ant 1.5.2 */ protected void zipFile(File file, ZipOutputStream zOut, String vPath, int mode) throws IOException { if (file.equals(zipFile)) { throw new BuildException("A zip file cannot include itself", getLocation()); } FileInputStream fIn = new FileInputStream(file); try { // ZIPs store time with a granularity of 2 seconds, round up zipFile(fIn, zOut, vPath, file.lastModified() + (roundUp ? ROUNDUP_MILLIS : 0), null, mode); } finally { fIn.close(); } } /** * Ensure all parent dirs of a given entry have been added. * @param baseDir the base directory to use (may be null) * @param entry the entry name to create directories from * @param zOut the stream to write to * @param prefix a prefix to place on the created entries * @param dirMode the directory mode * @throws IOException on error * @since Ant 1.5.2 */ protected final void addParentDirs(File baseDir, String entry, ZipOutputStream zOut, String prefix, int dirMode) throws IOException { if (!doFilesonly) { Stack directories = new Stack(); int slashPos = entry.length(); while ((slashPos = entry.lastIndexOf('/', slashPos - 1)) != -1) { String dir = entry.substring(0, slashPos + 1); if (addedDirs.get(prefix + dir) != null) { break; } directories.push(dir); } while (!directories.isEmpty()) { String dir = (String) directories.pop(); File f = null; if (baseDir != null) { f = new File(baseDir, dir); } else { f = new File(dir); } zipDir(f, zOut, prefix + dir, dirMode); } } } /** * Do any clean up necessary to allow this instance to be used again. * * <p>When we get here, the Zip file has been closed and all we * need to do is to reset some globals.</p> * * <p>This method will only reset globals that have been changed * during execute(), it will not alter the attributes or nested * child elements. If you want to reset the instance so that you * can later zip a completely different set of files, you must use * the reset method.</p> * * @see #reset */ protected void cleanUp() { addedDirs.clear(); addedFiles.removeAllElements(); entries.clear(); addingNewFiles = false; doUpdate = savedDoUpdate; Enumeration e = filesetsFromGroupfilesets.elements(); while (e.hasMoreElements()) { ZipFileSet zf = (ZipFileSet) e.nextElement(); resources.removeElement(zf); } filesetsFromGroupfilesets.removeAllElements(); } /** * Makes this instance reset all attributes to their default * values and forget all children. * * @since Ant 1.5 * * @see #cleanUp */ public void reset() { resources.removeAllElements(); zipFile = null; baseDir = null; groupfilesets.removeAllElements(); duplicate = "add"; archiveType = "zip"; doCompress = true; emptyBehavior = "skip"; doUpdate = false; doFilesonly = false; encoding = null; } /** * Check is the resource arrays are empty. * @param r the arrays to check * @return true if all individual arrays are empty * * @since Ant 1.5.2 */ protected static final boolean isEmpty(Resource[][] r) { for (int i = 0; i < r.length; i++) { if (r[i].length > 0) { return false; } } return true; } /** * Drops all non-file resources from the given array. * @param orig the resources to filter * @return the filters resources * @since Ant 1.6 */ protected Resource[] selectFileResources(Resource[] orig) { if (orig.length == 0) { return orig; } Vector v = new Vector(orig.length); for (int i = 0; i < orig.length; i++) { if (!orig[i].isDirectory()) { v.addElement(orig[i]); } else { log("Ignoring directory " + orig[i].getName() + " as only files will be added.", Project.MSG_VERBOSE); } } if (v.size() != orig.length) { Resource[] r = new Resource[v.size()]; v.copyInto(r); return r; } return orig; } /** * Possible behaviors when a duplicate file is added: * "add", "preserve" or "fail" */ public static class Duplicate extends EnumeratedAttribute { /** * @see EnumeratedAttribute#getValues() */ /** {@inheritDoc} */ public String[] getValues() { return new String[] {"add", "preserve", "fail"}; } } /** * Holds the up-to-date status and the out-of-date resources of * the original archive. * * @since Ant 1.5.3 */ public static class ArchiveState { private boolean outOfDate; private Resource[][] resourcesToAdd; ArchiveState(boolean state, Resource[][] r) { outOfDate = state; resourcesToAdd = r; } /** * Return the outofdate status. * @return the outofdate status */ public boolean isOutOfDate() { return outOfDate; } /** * Get the resources to add. * @return the resources to add */ public Resource[][] getResourcesToAdd() { return resourcesToAdd; } /** * find out if there are absolutely no resources to add * @since Ant 1.6.3 * @return true if there are no resources to add */ public boolean isWithoutAnyResources() { if (resourcesToAdd == null) { return true; } for (int counter = 0; counter < resourcesToAdd.length; counter++) { if (resourcesToAdd[counter] != null) { if (resourcesToAdd[counter].length > 0) { return false; } } } return true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -