use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tika by apache.
the class ZipWriter method zipStoreBuffer.
private static void zipStoreBuffer(ZipArchiveOutputStream zip, String name, byte[] dataBuffer) throws IOException {
ZipEntry zipEntry = new ZipEntry(name != null ? name : UUID.randomUUID().toString());
zipEntry.setMethod(ZipOutputStream.STORED);
zipEntry.setSize(dataBuffer.length);
CRC32 crc32 = new CRC32();
crc32.update(dataBuffer);
zipEntry.setCrc(crc32.getValue());
try {
zip.putArchiveEntry(new ZipArchiveEntry(zipEntry));
} catch (ZipException ex) {
if (name != null) {
zipStoreBuffer(zip, "x-" + name, dataBuffer);
return;
}
}
zip.write(dataBuffer);
zip.closeArchiveEntry();
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tomee by apache.
the class BuildTomEEMojo method zip.
private void zip(final ZipArchiveOutputStream zip, final File f, final String prefix) throws IOException {
final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
final ZipArchiveEntry archiveEntry = new ZipArchiveEntry(f, path);
if (isSh(path)) {
archiveEntry.setUnixMode(0755);
}
zip.putArchiveEntry(archiveEntry);
if (f.isDirectory()) {
zip.closeArchiveEntry();
final File[] files = f.listFiles();
if (files != null) {
for (final File child : files) {
zip(zip, child, prefix);
}
}
} else {
IO.copy(f, zip);
zip.closeArchiveEntry();
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project stanbol by apache.
the class ResourceLoader method loadResource.
/**
* Loads a resource from a file
* @param file the file resource
*/
private void loadResource(String file) {
synchronized (files) {
//sync to files to avoid two threads loading the same file
ResourceState state = files.get(file);
if (state == null || state != ResourceState.REGISTERED) {
log.info("Do not load File {} because of its state {} (null means removed from list)", file, state);
//someone removed it in between
return;
} else {
//set to loading
setResourceState(file, ResourceState.LOADING, null);
}
}
long startFile = System.currentTimeMillis();
log.info(" > loading '{}' ...", file);
String extension = FilenameUtils.getExtension(file);
if (loadEntriesWithinZipArchives && ("zip".equalsIgnoreCase(extension) || "jar".equalsIgnoreCase(extension))) {
log.info(" - processing {}-archive entries:", extension);
ZipFile zipArchive;
try {
zipArchive = new ZipFile(file);
} catch (IOException e) {
zipArchive = null;
setResourceState(file, ResourceState.ERROR, e);
}
if (zipArchive != null) {
boolean isError = false;
Enumeration<ZipArchiveEntry> entries = zipArchive.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
String entryName = entry.getName();
log.info(" o loading entry '{}'", entryName);
try {
ResourceState state = resourceImporter.importResource(zipArchive.getInputStream(entry), FilenameUtils.getName(entryName));
if (state == ResourceState.ERROR) {
isError = true;
}
} catch (IOException e) {
isError = true;
}
}
}
//set the state for the Archive as a whole
setResourceState(file, isError ? ResourceState.ERROR : ResourceState.LOADED, null);
}
} else {
InputStream is;
try {
is = new FileInputStream(file);
ResourceState state = resourceImporter.importResource(is, FilenameUtils.getName(file));
setResourceState(file, state, null);
} catch (FileNotFoundException e) {
//during init it is checked that files exists and are files
//and there is read access so this can only happen if
//someone deletes the file in between
log.warn("Unable to load resource " + file, e);
setResourceState(file, ResourceState.ERROR, e);
} catch (IOException e) {
log.error("Unable to load resource " + file, e);
setResourceState(file, ResourceState.ERROR, e);
} catch (Exception e) {
log.error("Unable to load resource " + file, e);
setResourceState(file, ResourceState.ERROR, e);
}
}
log.info(" - completed in {} seconds", (System.currentTimeMillis() - startFile) / 1000);
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tika by apache.
the class PackageParser method parseEntry.
private void parseEntry(ArchiveInputStream archive, ArchiveEntry entry, EmbeddedDocumentExtractor extractor, Metadata parentMetadata, XHTMLContentHandler xhtml) throws SAXException, IOException, TikaException {
String name = entry.getName();
if (archive.canReadEntryData(entry)) {
// Fetch the metadata on the entry contained in the archive
Metadata entrydata = handleEntryMetadata(name, null, entry.getLastModifiedDate(), entry.getSize(), xhtml);
// Recurse into the entry if desired
if (extractor.shouldParseEmbedded(entrydata)) {
// For detectors to work, we need a mark/reset supporting
// InputStream, which ArchiveInputStream isn't, so wrap
TemporaryResources tmp = new TemporaryResources();
try {
TikaInputStream tis = TikaInputStream.get(archive, tmp);
extractor.parseEmbedded(tis, xhtml, entrydata, true);
} finally {
tmp.dispose();
}
}
} else {
name = (name == null) ? "" : name;
if (entry instanceof ZipArchiveEntry) {
boolean usesEncryption = ((ZipArchiveEntry) entry).getGeneralPurposeBit().usesEncryption();
if (usesEncryption) {
EmbeddedDocumentUtil.recordEmbeddedStreamException(new EncryptedDocumentException("stream (" + name + ") is encrypted"), parentMetadata);
}
} else {
EmbeddedDocumentUtil.recordEmbeddedStreamException(new TikaException("Can't read archive stream (" + name + ")"), parentMetadata);
}
if (name.length() > 0) {
xhtml.element("p", name);
}
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project tika by apache.
the class ZipContainerDetector method detectIpa.
@SuppressWarnings("unchecked")
private static MediaType detectIpa(ZipFile zip) {
// Note - consider generalising this logic, if another format needs many regexp matching
Set<Pattern> tmpPatterns = (Set<Pattern>) ipaEntryPatterns.clone();
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
String name = entry.getName();
Iterator<Pattern> ip = tmpPatterns.iterator();
while (ip.hasNext()) {
if (ip.next().matcher(name).matches()) {
ip.remove();
}
}
if (tmpPatterns.isEmpty()) {
// We've found everything we need to find
return MediaType.application("x-itunes-ipa");
}
}
// If we get here, not all required entries were found
return null;
}
Aggregations