> From: Pavel Krupets [mailto:[EMAIL PROTECTED] > > Problem is that it always unzip files. I want to check it before > unzipping.
Pavel, I've used the custom extension of <unzip> included below to deal with pretty much the same issue you're faced with. Just compile and taskdef it to start using it instead of Ant's regular unzip, and it will only unzip when new or modified files are in the zip/jar compared to the extracted files on disk, and thus <jar> will also only re-jar when something changes. One caveat though, is that it doesn't 'sync', i.e. if a file is removed from the zip (jar), if won't remove it from disk, so you'll keep on jarring it. --DD import java.io.File; import java.io.IOException; import java.util.Date; import java.util.Enumeration; import org.apache.tools.ant.Project; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.Expand; import org.apache.tools.ant.types.PatternSet; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipEntry; /** * A smarter <unzip>. * <p> * This version differs from the Ant one in that it preserves the zip entries * modification dates and only expands zip entries which are different on disk * compared to what the archive recorded (name, type, size, last modification * date). It currently does not use the recorded CRC32 hash code against the * (would be) computed CRC32 of the disk file though. * * @author <a href="mailto:[EMAIL PROTECTED]">Dominique Devienne</a> * @version Apr 2004 */ public class Unzip extends Expand { /** Whether this unzip filters what gets extracted. */ private boolean _hasPatterns; /** * Expands a given ZIP file into the given directory. * <p> * Unfortunately, I need to duplicate the Ant method... * * @param fileUtils the file utils instance to use * @param srcF the source Zip file. * @param dir the target destination directory. */ protected void expandFile(FileUtils fileUtils, File srcF, File dir) { ZipFile zf = null; try { int expandedEntryCount = 0; zf = new ZipFile(srcF, "UTF-8"); // can't access encoding! for (Enumeration e = zf.getEntries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); // Systematically skip directories. Files expanded will // have their parent dirs created automatically anyways. if (entry.isDirectory()) { continue; } File entryFile = fileUtils.resolveFile(dir, entry.getName()); // FIXME: Maybe we should do CRC checking too... if (entryFile.exists() && entryFile.length() == entry.getSize() && entryFile.lastModified() == entry.getTime()) { log("Skipping " + entryFile + " as it is up-to-date", Project.MSG_DEBUG); continue; } if (++expandedEntryCount == 1) { log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO); } // Delegate actual extraction to Ant's <unzip> extractFile(fileUtils, srcF, dir, zf.getInputStream(entry), entry.getName(), new Date(entry.getTime()), entry.isDirectory()); if (!entryFile.setLastModified(entry.getTime())) { // Output warning only if we extract all files. // A ZIP entry could have been filtered out by // a patternset, and we have no way to know... int logLevel = _hasPatterns? Project.MSG_VERBOSE : Project.MSG_WARN; log("Cannot preserve last modified: " + entryFile, logLevel); } } log("expand complete", Project.MSG_VERBOSE); } catch (IOException ioe) { throw new BuildException("Error while expanding " + srcF.getPath(), ioe); } finally { if (zf != null) { try { zf.close(); } catch (IOException e) { //ignore } } } } /** * Forbids setting the overwrite mode. * * @param b ignored! * @throws BuildException always! */ public void setOverwrite(boolean b) { throw new BuildException("Not supported by <bm:unzip>"); } /** * Forbids setting the encoding. * <p> * The encoding is hard-coded to UTF-8. * * @param encoding ignored! * @throws BuildException always! */ public void setEncoding(String encoding) { throw new BuildException("Not supported by <bm:unzip>"); } /** * Adds a patternset. * * @param set the patternset used to filter what gets extracted. */ public void addPatternset(PatternSet set) { _hasPatterns = true; super.addPatternset(set); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]