use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project apex-core by apache.
the class AppPackage method extractToDirectory.
private static Manifest extractToDirectory(File directory, ZipArchiveInputStream input) throws IOException {
Manifest manifest = null;
File manifestFile = new File(directory, JarFile.MANIFEST_NAME);
manifestFile.getParentFile().mkdirs();
ZipArchiveEntry entry = input.getNextZipEntry();
while (entry != null) {
File newFile = new File(directory, entry.getName());
if (entry.isDirectory()) {
newFile.mkdirs();
} else {
if (JarFile.MANIFEST_NAME.equals(entry.getName())) {
manifest = new Manifest(input);
try (FileOutputStream output = new FileOutputStream(newFile)) {
manifest.write(output);
}
} else {
try (FileOutputStream output = new FileOutputStream(newFile)) {
IOUtils.copy(input, output);
}
}
}
entry = input.getNextZipEntry();
}
return manifest;
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project gerrit by GerritCodeReview.
the class UploadArchiveIT method zipFormat.
@Test
public void zipFormat() throws Exception {
PushOneCommit.Result r = createChange();
String abbreviated = r.getCommit().abbreviate(8).name();
String c = command(r, abbreviated);
InputStream out = adminSshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c));
// Wrap with PacketLineIn to read ACK bytes from output stream
PacketLineIn in = new PacketLineIn(out);
String tmp = in.readString();
assertThat(tmp).isEqualTo("ACK");
tmp = in.readString();
// Skip length (4 bytes) + 1 byte
// to position the output stream to the raw zip stream
byte[] buffer = new byte[5];
IO.readFully(out, buffer, 0, 5);
Set<String> entryNames = new TreeSet<>();
try (ZipArchiveInputStream zip = new ZipArchiveInputStream(out)) {
ZipArchiveEntry zipEntry = zip.getNextZipEntry();
while (zipEntry != null) {
String name = zipEntry.getName();
entryNames.add(name);
zipEntry = zip.getNextZipEntry();
}
}
assertThat(entryNames.size()).isEqualTo(1);
assertThat(Iterables.getOnlyElement(entryNames)).isEqualTo(String.format("%s/%s", abbreviated, PushOneCommit.FILE_NAME));
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project streamsx.topology by IBMStreams.
the class ZippedToolkitRemoteContext method addAllToZippedArchive.
private static void addAllToZippedArchive(Map<Path, String> starts, Path zipFilePath) throws IOException {
try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(zipFilePath.toFile())) {
for (Path start : starts.keySet()) {
final String rootEntryName = starts.get(start);
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// Skip pyc files.
if (file.getFileName().toString().endsWith(".pyc"))
return FileVisitResult.CONTINUE;
String entryName = rootEntryName;
String relativePath = start.relativize(file).toString();
// If empty, file is the start file.
if (!relativePath.isEmpty()) {
entryName = entryName + "/" + relativePath;
}
// Zip uses forward slashes
entryName = entryName.replace(File.separatorChar, '/');
ZipArchiveEntry entry = new ZipArchiveEntry(file.toFile(), entryName);
if (Files.isExecutable(file))
entry.setUnixMode(0100770);
else
entry.setUnixMode(0100660);
zos.putArchiveEntry(entry);
Files.copy(file, zos);
zos.closeArchiveEntry();
return FileVisitResult.CONTINUE;
}
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
final String dirName = dir.getFileName().toString();
// Don't include pyc files or .toolkit
if (dirName.equals("__pycache__"))
return FileVisitResult.SKIP_SUBTREE;
ZipArchiveEntry dirEntry = new ZipArchiveEntry(dir.toFile(), rootEntryName + "/" + start.relativize(dir).toString().replace(File.separatorChar, '/') + "/");
zos.putArchiveEntry(dirEntry);
zos.closeArchiveEntry();
return FileVisitResult.CONTINUE;
}
});
}
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tika by apache.
the class ZipContainerDetector method detectKmz.
private static MediaType detectKmz(ZipFile zip) {
boolean kmlFound = false;
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
String name = entry.getName();
if (!entry.isDirectory() && name.indexOf('/') == -1 && name.indexOf('\\') == -1) {
if (name.endsWith(".kml") && !kmlFound) {
kmlFound = true;
} else {
return null;
}
}
}
if (kmlFound) {
return MediaType.application("vnd.google-earth.kmz");
} else {
return null;
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project stanbol by apache.
the class ConfigUtils method copyCore.
/**
* Copy the configuration of an core.
*
* @param clazzInArchive
* This class is used to identify the archive containing the default configuration. Parsing
* <code>null</code> causes this class to be used and therefore initialises the default core
* configuration contained by the SolrYard bundle.
* @param coreDir
* the target directory for the core
* @param coreName
* the core name or <code>null</code> to directly load the configuration as present under
* {@value #CONFIG_DIR} in the bundle. This property can be used if a bundle needs to provide
* multiple core configurations
* @param override
* if files in the target directory should be overridden
* @throws IOException
* On any IO error while coping the configuration
* @throws IllegalStateException
* If the parsed coreDir does exist but is not a directory.
* @throws IllegalArgumentException
* if <code>null</code> is parsed as coreDir or if the parsed bundle does not contain the
* required information to set up an configuration or the parsed coreName is empty.
*/
public static void copyCore(Class<?> clazzInArchive, File coreDir, String coreName, boolean override) throws IOException, IllegalArgumentException, IllegalStateException {
if (coreDir == null) {
throw new IllegalArgumentException("The parsed core directory MUST NOT be NULL!");
}
if (coreDir.exists() && !coreDir.isDirectory()) {
throw new IllegalStateException("The parsed core directory " + coreDir.getAbsolutePath() + " extists but is not a directory!");
}
if (coreName != null && coreName.isEmpty()) {
throw new IllegalArgumentException("The parsed core name MUST NOT be empty (However NULL is supported)!");
}
String context = CORE_CONFIG_DIR + (coreName != null ? '/' + coreName : "");
File sourceRoot = getSource(clazzInArchive != null ? clazzInArchive : ConfigUtils.class);
if (sourceRoot.isFile()) {
ZipFile archive = new ZipFile(sourceRoot);
log.info(String.format("Copy core %s config from jar-file %s to %s (override=%s)", (coreName == null ? "" : coreName), sourceRoot.getName(), coreDir.getAbsolutePath(), override));
try {
for (@SuppressWarnings("unchecked") Enumeration<ZipArchiveEntry> entries = archive.getEntries(); entries.hasMoreElements(); ) {
ZipArchiveEntry entry = entries.nextElement();
if (entry.getName().startsWith(context)) {
copyResource(coreDir, archive, entry, context, override);
}
}
} finally {
// regardless what happens we need to close the archive!
ZipFile.closeQuietly(archive);
}
} else {
// load from file
File source = new File(sourceRoot, context);
if (source.exists() && source.isDirectory()) {
FileUtils.copyDirectory(source, coreDir);
} else {
throw new FileNotFoundException("The SolrIndex default config was not found in directory " + source.getAbsolutePath());
}
}
}
Aggregations