use of org.apache.tools.ant.types.resources.ZipResource in project ant by apache.
the class ZipScanner method fillMapsFromArchive.
/**
* Fills the file and directory maps with resources read from the
* archive.
*
* @param src the archive to scan.
* @param encoding encoding used to encode file names inside the archive.
* @param fileEntries Map (name to resource) of non-directory
* resources found inside the archive.
* @param matchFileEntries Map (name to resource) of non-directory
* resources found inside the archive that matched all include
* patterns and didn't match any exclude patterns.
* @param dirEntries Map (name to resource) of directory
* resources found inside the archive.
* @param matchDirEntries Map (name to resource) of directory
* resources found inside the archive that matched all include
* patterns and didn't match any exclude patterns.
*/
@Override
protected void fillMapsFromArchive(Resource src, String encoding, Map<String, Resource> fileEntries, Map<String, Resource> matchFileEntries, Map<String, Resource> dirEntries, Map<String, Resource> matchDirEntries) {
File srcFile = src.asOptional(FileProvider.class).map(FileProvider::getFile).orElseThrow(() -> new BuildException("Only file provider resources are supported"));
try (ZipFile zf = new ZipFile(srcFile, encoding)) {
Enumeration<ZipEntry> e = zf.getEntries();
while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement();
Resource r = new ZipResource(srcFile, encoding, entry);
String name = entry.getName();
if (entry.isDirectory()) {
name = trimSeparator(name);
dirEntries.put(name, r);
if (match(name)) {
matchDirEntries.put(name, r);
}
} else {
fileEntries.put(name, r);
if (match(name)) {
matchFileEntries.put(name, r);
}
}
}
} catch (ZipException ex) {
throw new BuildException("Problem reading " + srcFile, ex);
} catch (IOException ex) {
throw new BuildException("Problem opening " + srcFile, ex);
}
}
use of org.apache.tools.ant.types.resources.ZipResource in project ant by apache.
the class ResourceOutputTest method testzipentryoutput.
@Test
public void testzipentryoutput() {
Zip z = new Zip();
z.setProject(project);
Zip.WhenEmpty create = new Zip.WhenEmpty();
create.setValue("create");
z.setWhenempty(create);
z.setBasedir(basedir);
z.setExcludes("**/*");
File f = project.resolveFile("foo");
z.setDestFile(f);
z.execute();
ZipResource r = new ZipResource();
r.setZipfile(f);
r.setName("foo");
try {
testoutputbe(r);
fail("should have caught UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// TODO assert exception message
} finally {
if (!f.delete()) {
f.deleteOnExit();
}
}
}
use of org.apache.tools.ant.types.resources.ZipResource in project ant by apache.
the class PermissionUtilsTest method getSetPermissionsWorksForZipResources.
@Test
public void getSetPermissionsWorksForZipResources() throws IOException {
File f = File.createTempFile("ant", ".zip");
f.deleteOnExit();
try (ZipOutputStream os = new ZipOutputStream(f)) {
ZipEntry e = new ZipEntry("foo");
os.putNextEntry(e);
os.closeEntry();
}
ZipResource r = new ZipResource();
r.setName("foo");
r.setArchive(f);
Set<PosixFilePermission> s = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ);
PermissionUtils.setPermissions(r, s, null);
assertEquals(s, PermissionUtils.getPermissions(r, null));
}
use of org.apache.tools.ant.types.resources.ZipResource in project ant by apache.
the class ZipExtraFieldTest method testExtraField.
private void testExtraField(Zip testInstance, boolean expectZip64) throws IOException {
File f = File.createTempFile("ziptest", ".zip");
f.delete();
ZipFile zf = null;
try {
testInstance.setDestFile(f);
final ZipResource r = new ZipResource() {
public String getName() {
return "x";
}
public boolean isExists() {
return true;
}
public boolean isDirectory() {
return false;
}
public long getLastModified() {
return 1;
}
public InputStream getInputStream() {
return new ByteArrayInputStream(new byte[0]);
}
public ZipExtraField[] getExtraFields() {
return new ZipExtraField[] { new JarMarker() };
}
};
testInstance.add(new ResourceCollection() {
public boolean isFilesystemOnly() {
return false;
}
public int size() {
return 1;
}
public Iterator<Resource> iterator() {
return Collections.<Resource>singleton(r).iterator();
}
});
testInstance.execute();
zf = new ZipFile(f);
ZipEntry ze = zf.getEntry("x");
assertNotNull(ze);
assertEquals(expectZip64 ? 2 : 1, ze.getExtraFields().length);
assertTrue(ze.getExtraFields()[0] instanceof JarMarker);
if (expectZip64) {
assertTrue(ze.getExtraFields()[1] instanceof Zip64ExtendedInformationExtraField);
}
} finally {
ZipFile.closeQuietly(zf);
if (f.exists()) {
f.delete();
}
}
}
Aggregations