Search in sources :

Example 71 with ZipFile

use of java.util.zip.ZipFile in project jodd by oblac.

the class ZipUtilTest method testZipBuilderFileMemory.

@Test
public void testZipBuilderFileMemory() throws IOException {
    byte[] bytes = ZipBuilder.createZipInMemory().add(new File(dataRoot, "sb.data")).path("sbdata").comment("This is sb data file").save().add(new File(dataRoot, "file")).path("folder").comment("This is a folder and all its files").save().add("text").path("folder/txt").save().addFolder("folder2").add("txet").path("folder2/txt2").save().toBytes();
    File zipFile = new File(dataRoot, "test.zip");
    FileUtil.writeBytes(zipFile, bytes);
    assertTrue(zipFile.exists());
    ZipUtil.unzip(zipFile, new File(dataRoot));
    assertTrue(new File(dataRoot, "sbdata").exists());
    assertTrue(new File(dataRoot, "folder").exists());
    assertTrue(new File(dataRoot, "folder").isDirectory());
    assertTrue(new File(dataRoot, "folder2").exists());
    assertTrue(new File(dataRoot, "folder2").isDirectory());
    assertTrue(new File(new File(dataRoot, "folder"), "txt").exists());
    assertEquals("text", FileUtil.readString(new File(new File(dataRoot, "folder"), "txt")));
    assertTrue(new File(new File(dataRoot, "folder2"), "txt2").exists());
    assertEquals("txet", FileUtil.readString(new File(new File(dataRoot, "folder2"), "txt2")));
    // cleanup
    FileUtil.delete(new File(dataRoot, "sbdata"));
    FileUtil.deleteDir(new File(dataRoot, "folder"));
    FileUtil.deleteDir(new File(dataRoot, "folder2"));
    FileUtil.delete(zipFile);
}
Also used : File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 72 with ZipFile

use of java.util.zip.ZipFile in project jodd by oblac.

the class ZipUtilTest method testZipEmptyFolder.

@Test
public void testZipEmptyFolder() throws IOException {
    byte[] bytes = ZipBuilder.createZipInMemory().addFolder("myEmptyFolder").toBytes();
    File tempDir = FileUtil.createTempDirectory();
    tempDir.deleteOnExit();
    File zipFile = new File(tempDir, "test.zip");
    FileUtil.writeBytes(zipFile, bytes);
    // read zip
    List<String> entries = ZipUtil.listZip(zipFile);
    assertEquals(1, entries.size());
    assertEquals("myEmptyFolder/", entries.get(0));
}
Also used : File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 73 with ZipFile

use of java.util.zip.ZipFile in project jodd by oblac.

the class ZipUtil method listZip.

// ---------------------------------------------------------------- unzip
/**
	 * Lists zip content.
	 */
public static List<String> listZip(File zipFile) throws IOException {
    List<String> entries = new ArrayList<>();
    ZipFile zip = new ZipFile(zipFile);
    Enumeration zipEntries = zip.entries();
    while (zipEntries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) zipEntries.nextElement();
        String entryName = entry.getName();
        entries.add(entryName);
    }
    return Collections.unmodifiableList(entries);
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList)

Example 74 with ZipFile

use of java.util.zip.ZipFile in project jodd by oblac.

the class ClassFinder method scanJarFile.

// ---------------------------------------------------------------- internal
/**
	 * Scans classes inside single JAR archive. Archive is scanned as a zip file.
	 * @see #onEntry(EntryData)
	 */
protected void scanJarFile(File file) {
    ZipFile zipFile;
    try {
        zipFile = new ZipFile(file);
    } catch (IOException ioex) {
        if (!ignoreException) {
            throw new FindFileException("Invalid zip: " + file.getName(), ioex);
        }
        return;
    }
    Enumeration entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) entries.nextElement();
        String zipEntryName = zipEntry.getName();
        try {
            if (StringUtil.endsWithIgnoreCase(zipEntryName, CLASS_FILE_EXT)) {
                String entryName = prepareEntryName(zipEntryName, true);
                EntryData entryData = new EntryData(entryName, zipFile, zipEntry);
                try {
                    scanEntry(entryData);
                } finally {
                    entryData.closeInputStreamIfOpen();
                }
            } else if (includeResources) {
                String entryName = prepareEntryName(zipEntryName, false);
                EntryData entryData = new EntryData(entryName, zipFile, zipEntry);
                try {
                    scanEntry(entryData);
                } finally {
                    entryData.closeInputStreamIfOpen();
                }
            }
        } catch (RuntimeException rex) {
            if (!ignoreException) {
                ZipUtil.close(zipFile);
                throw rex;
            }
        }
    }
    ZipUtil.close(zipFile);
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 75 with ZipFile

use of java.util.zip.ZipFile in project robovm by robovm.

the class AbstractTarget method stripArchive.

protected void stripArchive(Path path, File output) throws IOException {
    if (!config.isClean() && output.exists() && !path.hasChangedSince(output.lastModified())) {
        config.getLogger().info("Not creating stripped archive file %s for unchanged path %s", output, path.getFile());
        return;
    }
    config.getLogger().info("Creating stripped archive file %s", output);
    ZipOutputStream out = null;
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(output)));
        if (path.getFile().isFile()) {
            ZipFile archive = null;
            try {
                archive = new ZipFile(path.getFile());
                Enumeration<? extends ZipEntry> entries = archive.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    if (entry.getName().toLowerCase().endsWith(".class")) {
                        continue;
                    }
                    if (entry.getName().startsWith("META-INF/robovm/")) {
                        // Don't include anything under META-INF/robovm/
                        continue;
                    }
                    ZipEntry newEntry = new ZipEntry(entry.getName());
                    newEntry.setTime(entry.getTime());
                    out.putNextEntry(newEntry);
                    InputStream in = null;
                    try {
                        in = archive.getInputStream(entry);
                        IOUtils.copy(in, out);
                        out.closeEntry();
                    } finally {
                        IOUtils.closeQuietly(in);
                    }
                }
            } finally {
                try {
                    archive.close();
                } catch (Throwable t) {
                }
            }
        } else {
            String basePath = path.getFile().getAbsolutePath();
            @SuppressWarnings("unchecked") Collection<File> files = FileUtils.listFiles(path.getFile(), null, true);
            for (File f : files) {
                if (f.getName().toLowerCase().endsWith(".class")) {
                    continue;
                }
                String entryName = f.getAbsolutePath().substring(basePath.length() + 1);
                if (entryName.startsWith("META-INF/robovm/")) {
                    // Don't include anything under META-INF/robovm/
                    continue;
                }
                ZipEntry newEntry = new ZipEntry(entryName);
                newEntry.setTime(f.lastModified());
                out.putNextEntry(newEntry);
                InputStream in = null;
                try {
                    in = new FileInputStream(f);
                    IOUtils.copy(in, out);
                    out.closeEntry();
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }
    } catch (IOException e) {
        IOUtils.closeQuietly(out);
        output.delete();
    } finally {
        IOUtils.closeQuietly(out);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

ZipFile (java.util.zip.ZipFile)580 ZipEntry (java.util.zip.ZipEntry)414 File (java.io.File)261 IOException (java.io.IOException)189 InputStream (java.io.InputStream)127 FileOutputStream (java.io.FileOutputStream)98 ZipOutputStream (java.util.zip.ZipOutputStream)89 Test (org.junit.Test)88 FileInputStream (java.io.FileInputStream)57 ArrayList (java.util.ArrayList)44 Enumeration (java.util.Enumeration)42 BufferedInputStream (java.io.BufferedInputStream)38 BufferedOutputStream (java.io.BufferedOutputStream)35 ZipException (java.util.zip.ZipException)32 ClassReader (org.objectweb.asm.ClassReader)29 OutputStream (java.io.OutputStream)27 JarFile (java.util.jar.JarFile)26 ZipInputStream (java.util.zip.ZipInputStream)26 FileNotFoundException (java.io.FileNotFoundException)23 Path (java.nio.file.Path)23