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