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;
}
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;
}
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();
}
}
Aggregations