Search in sources :

Example 1 with ArchiveFileSet

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

the class Zip method addResources.

/**
 * 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(final FileSet fileset, final Resource[] resources, final 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.");
    }
    if (!prefix.isEmpty()) {
        if (!prefix.endsWith("/") && !prefix.endsWith("\\")) {
            prefix += "/";
        }
        addParentDirs(null, prefix, zOut, "", dirMode);
    }
    ZipFile zf = null;
    try {
        boolean dealingWithFiles = false;
        File base = null;
        if (zfs == null || zfs.getSrc(getProject()) == null) {
            dealingWithFiles = true;
            base = fileset.getDir(getProject());
        } else if (zfs instanceof ZipFileSet) {
            zf = new ZipFile(zfs.getSrc(getProject()), encoding);
        }
        for (Resource resource : resources) {
            String name;
            if (fullpath.isEmpty()) {
                name = resource.getName();
            } else {
                name = fullpath;
            }
            name = name.replace(File.separatorChar, '/');
            if (name.isEmpty()) {
                continue;
            }
            if (resource.isDirectory()) {
                if (doFilesonly) {
                    continue;
                }
                final int thisDirMode = zfs != null && zfs.hasDirModeBeenSet() ? dirMode : getUnixMode(resource, zf, dirMode);
                addDirectoryResource(resource, name, prefix, base, zOut, dirMode, thisDirMode);
            } else {
                // !isDirectory
                addParentDirs(base, name, zOut, prefix, dirMode);
                if (dealingWithFiles) {
                    final File f = FILE_UTILS.resolveFile(base, resource.getName());
                    zipFile(f, zOut, prefix + name, fileMode);
                } else {
                    final int thisFileMode = zfs != null && zfs.hasFileModeBeenSet() ? fileMode : getUnixMode(resource, zf, fileMode);
                    addResource(resource, name, prefix, zOut, thisFileMode, zf, zfs == null ? null : zfs.getSrc(getProject()));
                }
            }
        }
    } finally {
        if (zf != null) {
            zf.close();
        }
    }
}
Also used : ZipFile(org.apache.tools.zip.ZipFile) 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) BuildException(org.apache.tools.ant.BuildException) ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 2 with ArchiveFileSet

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

the class Tar method tar.

/**
 * Adds the resources contained in this collection to the archive.
 *
 * <p>Uses the file based methods for file resources for backwards
 * compatibility.</p>
 *
 * @param rc the collection containing resources to add
 * @param tOut stream writing to the archive.
 * @throws IOException on error.
 * @since Ant 1.7
 */
protected void tar(final ResourceCollection rc, final TarOutputStream tOut) throws IOException {
    ArchiveFileSet afs = null;
    if (rc instanceof ArchiveFileSet) {
        afs = (ArchiveFileSet) rc;
    }
    if (afs != null && afs.size() > 1 && !afs.getFullpath(this.getProject()).isEmpty()) {
        throw new BuildException("fullpath attribute may only be specified for filesets that specify a single file.");
    }
    final TarFileSet tfs = asTarFileSet(afs);
    if (isFileFileSet(rc)) {
        final FileSet fs = (FileSet) rc;
        for (String file : getFileNames(fs)) {
            final File f = new File(fs.getDir(getProject()), file);
            final String name = file.replace(File.separatorChar, '/');
            tarFile(f, tOut, name, tfs);
        }
    } else if (rc.isFilesystemOnly()) {
        for (final Resource r : rc) {
            final File f = r.as(FileProvider.class).getFile();
            tarFile(f, tOut, f.getName(), tfs);
        }
    } else {
        // non-file resources
        for (final Resource r : rc) {
            tarResource(r, tOut, r.getName(), tfs);
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) FileResource(org.apache.tools.ant.types.resources.FileResource) TarResource(org.apache.tools.ant.types.resources.TarResource) Resource(org.apache.tools.ant.types.Resource) BuildException(org.apache.tools.ant.BuildException) ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) File(java.io.File)

Aggregations

File (java.io.File)2 BuildException (org.apache.tools.ant.BuildException)2 ArchiveFileSet (org.apache.tools.ant.types.ArchiveFileSet)2 Resource (org.apache.tools.ant.types.Resource)2 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)2 FileResource (org.apache.tools.ant.types.resources.FileResource)2 FileSet (org.apache.tools.ant.types.FileSet)1 ZipFileSet (org.apache.tools.ant.types.ZipFileSet)1 TarResource (org.apache.tools.ant.types.resources.TarResource)1 ZipResource (org.apache.tools.ant.types.resources.ZipResource)1 ZipFile (org.apache.tools.zip.ZipFile)1