Search in sources :

Example 1 with ZipResource

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);
    }
}
Also used : ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipFile(org.apache.tools.zip.ZipFile) ZipEntry(org.apache.tools.zip.ZipEntry) ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipException(java.util.zip.ZipException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 2 with ZipResource

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();
        }
    }
}
Also used : Zip(org.apache.tools.ant.taskdefs.Zip) ZipResource(org.apache.tools.ant.types.resources.ZipResource) File(java.io.File) Test(org.junit.Test)

Example 3 with ZipResource

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));
}
Also used : ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipOutputStream(org.apache.tools.zip.ZipOutputStream) ZipEntry(org.apache.tools.zip.ZipEntry) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File) Test(org.junit.Test)

Example 4 with ZipResource

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();
        }
    }
}
Also used : ZipExtraField(org.apache.tools.zip.ZipExtraField) ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipFile(org.apache.tools.zip.ZipFile) JarMarker(org.apache.tools.zip.JarMarker) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(org.apache.tools.zip.ZipEntry) Iterator(java.util.Iterator) Resource(org.apache.tools.ant.types.Resource) ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) Zip64ExtendedInformationExtraField(org.apache.tools.zip.Zip64ExtendedInformationExtraField)

Aggregations

File (java.io.File)4 ZipResource (org.apache.tools.ant.types.resources.ZipResource)4 ZipEntry (org.apache.tools.zip.ZipEntry)3 ZipFile (org.apache.tools.zip.ZipFile)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 Iterator (java.util.Iterator)1 ZipException (java.util.zip.ZipException)1 BuildException (org.apache.tools.ant.BuildException)1 Zip (org.apache.tools.ant.taskdefs.Zip)1 Resource (org.apache.tools.ant.types.Resource)1 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)1 JarMarker (org.apache.tools.zip.JarMarker)1 Zip64ExtendedInformationExtraField (org.apache.tools.zip.Zip64ExtendedInformationExtraField)1 ZipExtraField (org.apache.tools.zip.ZipExtraField)1 ZipOutputStream (org.apache.tools.zip.ZipOutputStream)1