Search in sources :

Example 1 with ZipScanner

use of org.apache.tools.ant.types.ZipScanner in project ant by apache.

the class Zip method grabResources.

/**
 * Fetch all included and not excluded resources from the sets.
 *
 * <p>Included directories will precede included files.</p>
 * @param filesets an array of filesets
 * @return the resources included
 * @since Ant 1.5.2
 */
protected Resource[][] grabResources(final FileSet[] filesets) {
    final Resource[][] result = new Resource[filesets.length][];
    for (int i = 0; i < filesets.length; i++) {
        boolean skipEmptyNames = true;
        if (filesets[i] instanceof ZipFileSet) {
            final ZipFileSet zfs = (ZipFileSet) filesets[i];
            skipEmptyNames = zfs.getPrefix(getProject()).isEmpty() && zfs.getFullpath(getProject()).isEmpty();
        }
        final DirectoryScanner rs = filesets[i].getDirectoryScanner(getProject());
        if (rs instanceof ZipScanner) {
            ((ZipScanner) rs).setEncoding(encoding);
        }
        final List<Resource> resources = new Vector<>();
        if (!doFilesonly) {
            for (String d : rs.getIncludedDirectories()) {
                if (!d.isEmpty() || !skipEmptyNames) {
                    resources.add(rs.getResource(d));
                }
            }
        }
        for (String f : rs.getIncludedFiles()) {
            if (!f.isEmpty() || !skipEmptyNames) {
                resources.add(rs.getResource(f));
            }
        }
        result[i] = resources.toArray(new Resource[resources.size()]);
    }
    return result;
}
Also used : DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipScanner(org.apache.tools.ant.types.ZipScanner) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) Vector(java.util.Vector)

Example 2 with ZipScanner

use of org.apache.tools.ant.types.ZipScanner in project ant by apache.

the class Zip method getZipScanner.

/**
 * @since Ant 1.5.2
 */
private synchronized ZipScanner getZipScanner() {
    if (zs == null) {
        zs = new ZipScanner();
        zs.setEncoding(encoding);
        zs.setSrc(zipFile);
    }
    return zs;
}
Also used : ZipScanner(org.apache.tools.ant.types.ZipScanner)

Example 3 with ZipScanner

use of org.apache.tools.ant.types.ZipScanner in project ant by apache.

the class Zip method executeMain.

/**
 * Build the zip file.
 * This is called twice if doubleFilePass is true.
 * @throws BuildException on error
 */
public void executeMain() throws BuildException {
    checkAttributesAndElements();
    // Renamed version of original file, if it exists
    File renamedFile = null;
    addingNewFiles = true;
    processDoUpdate();
    processGroupFilesets();
    // collect filesets to pass them to getResourcesToAdd
    final List<ResourceCollection> vfss = new ArrayList<>();
    if (baseDir != null) {
        final FileSet fs = (FileSet) getImplicitFileSet().clone();
        fs.setDir(baseDir);
        vfss.add(fs);
    }
    vfss.addAll(resources);
    final ResourceCollection[] fss = vfss.toArray(new ResourceCollection[vfss.size()]);
    boolean success = false;
    try {
        // can also handle empty archives
        final ArchiveState state = getResourcesToAdd(fss, zipFile, false);
        // quick exit if the target is up to date
        if (!state.isOutOfDate()) {
            return;
        }
        final File parent = zipFile.getParentFile();
        if (parent != null && !parent.isDirectory() && !(parent.mkdirs() || parent.isDirectory())) {
            throw new BuildException("Failed to create missing parent directory for %s", zipFile);
        }
        updatedFile = true;
        if (!zipFile.exists() && state.isWithoutAnyResources()) {
            createEmptyZip(zipFile);
            return;
        }
        final Resource[][] addThem = state.getResourcesToAdd();
        if (doUpdate) {
            renamedFile = renameFile();
        }
        final String action = doUpdate ? "Updating " : "Building ";
        if (!skipWriting) {
            log(action + archiveType + ": " + zipFile.getAbsolutePath());
        }
        ZipOutputStream zOut = null;
        try {
            if (!skipWriting) {
                zOut = new ZipOutputStream(zipFile);
                zOut.setEncoding(encoding);
                zOut.setUseLanguageEncodingFlag(useLanguageEncodingFlag);
                zOut.setCreateUnicodeExtraFields(createUnicodeExtraFields.getPolicy());
                zOut.setFallbackToUTF8(fallBackToUTF8);
                zOut.setMethod(doCompress ? ZipOutputStream.DEFLATED : ZipOutputStream.STORED);
                zOut.setLevel(level);
                zOut.setUseZip64(zip64Mode.getMode());
            }
            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;
                final ZipFileSet oldFiles = new ZipFileSet();
                oldFiles.setProject(getProject());
                oldFiles.setSrc(renamedFile);
                oldFiles.setDefaultexcludes(false);
                for (String addedFile : addedFiles) {
                    oldFiles.createExclude().setName(addedFile);
                }
                final DirectoryScanner ds = oldFiles.getDirectoryScanner(getProject());
                ((ZipScanner) ds).setEncoding(encoding);
                Stream<String> includedResourceNames = Stream.of(ds.getIncludedFiles());
                if (!doFilesonly) {
                    includedResourceNames = Stream.concat(includedResourceNames, Stream.of(ds.getIncludedDirectories()));
                }
                Resource[] r = includedResourceNames.map(ds::getResource).toArray(Resource[]::new);
                addResources(oldFiles, r, zOut);
            }
            if (zOut != null) {
                zOut.setComment(comment);
            }
            finalizeZipOutputStream(zOut);
            // 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.
            closeZout(zOut, success);
        }
    } catch (final 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 (final IOException e) {
                msg += " (and I couldn't rename the temporary file " + renamedFile.getName() + " back)";
            }
        }
        throw new BuildException(msg, ioe, getLocation());
    } finally {
        cleanUp();
    }
}
Also used : ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) FileSet(org.apache.tools.ant.types.FileSet) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ArrayList(java.util.ArrayList) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) ZipResource(org.apache.tools.ant.types.resources.ZipResource) IOException(java.io.IOException) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipOutputStream(org.apache.tools.zip.ZipOutputStream) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BuildException(org.apache.tools.ant.BuildException) ZipScanner(org.apache.tools.ant.types.ZipScanner) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Aggregations

ZipScanner (org.apache.tools.ant.types.ZipScanner)3 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)2 Resource (org.apache.tools.ant.types.Resource)2 ZipFileSet (org.apache.tools.ant.types.ZipFileSet)2 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)2 FileResource (org.apache.tools.ant.types.resources.FileResource)2 ZipResource (org.apache.tools.ant.types.resources.ZipResource)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Vector (java.util.Vector)1 BuildException (org.apache.tools.ant.BuildException)1 ArchiveFileSet (org.apache.tools.ant.types.ArchiveFileSet)1 FileSet (org.apache.tools.ant.types.FileSet)1 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)1 ZipFile (org.apache.tools.zip.ZipFile)1 ZipOutputStream (org.apache.tools.zip.ZipOutputStream)1