use of java.util.zip.ZipInputStream in project tika by apache.
the class EpubParser method parse.
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
// Because an EPub file is often made up of multiple XHTML files,
// we need explicit control over the start and end of the document
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
ContentHandler childHandler = new EmbeddedContentHandler(new BodyContentHandler(xhtml));
ZipInputStream zip = new ZipInputStream(stream);
ZipEntry entry = zip.getNextEntry();
while (entry != null) {
if (entry.getName().equals("mimetype")) {
String type = IOUtils.toString(zip, UTF_8);
//often has trailing new lines
if (type != null) {
type = type.trim();
}
metadata.set(Metadata.CONTENT_TYPE, type);
} else if (entry.getName().equals("metadata.xml")) {
meta.parse(zip, new DefaultHandler(), metadata, context);
} else if (entry.getName().endsWith(".opf")) {
meta.parse(zip, new DefaultHandler(), metadata, context);
} else if (entry.getName().endsWith(".html") || entry.getName().endsWith(".xhtml")) {
content.parse(zip, childHandler, metadata, context);
}
entry = zip.getNextEntry();
}
// Finish everything
xhtml.endDocument();
}
use of java.util.zip.ZipInputStream in project sling by apache.
the class PackageTransformer method checkForPackage.
/**
* Check if the resource is a content package
* @param resource The resource
* @return {@code null} if not a content package, a result otherwise
*/
private TransformationResult[] checkForPackage(final RegisteredResource resource) {
// first check if this is a zip archive
try (final ZipInputStream zin = new ZipInputStream(new BufferedInputStream(resource.getInputStream()))) {
if (zin.getNextEntry() == null) {
return null;
}
} catch (final IOException ioe) {
logger.debug("Unable to read resource.", ioe);
return null;
}
Session session = null;
JcrPackage pck = null;
try {
// create an admin session
session = repository.loginAdministrative(null);
final JcrPackageManager pckMgr = pkgSvc.getPackageManager(session);
pck = pckMgr.upload(resource.getInputStream(), true, true);
if (pck.isValid()) {
final PackageId pid = pck.getDefinition().getId();
final Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put(ATTR_PCK_ID, pid.toString());
final TransformationResult tr = new TransformationResult();
tr.setId(pid.getGroup() + ':' + pid.getName());
tr.setResourceType(RESOURCE_TYPE);
tr.setAttributes(attrs);
// version
final String version = pid.getVersionString();
if (version.length() > 0) {
tr.setVersion(new Version(cleanupVersion(version)));
}
return new TransformationResult[] { tr };
}
} catch (final Exception ioe) {
logger.debug("Unable to check content package " + resource.getURL(), ioe);
} finally {
if (pck != null) {
pck.close();
}
if (session != null) {
session.logout();
}
}
return null;
}
use of java.util.zip.ZipInputStream in project kotlin by JetBrains.
the class ClassEntry method addEntries.
/**
* Given a classpath, add all the class files found within the directories and inside jar files
*/
private static void addEntries(@NonNull LintClient client, @NonNull List<ClassEntry> entries, @NonNull List<File> classPath) {
for (File classPathEntry : classPath) {
if (classPathEntry.getName().endsWith(DOT_JAR)) {
//noinspection UnnecessaryLocalVariable
File jarFile = classPathEntry;
if (!jarFile.exists()) {
continue;
}
ZipInputStream zis = null;
try {
FileInputStream fis = new FileInputStream(jarFile);
try {
zis = new ZipInputStream(fis);
ZipEntry entry = zis.getNextEntry();
while (entry != null) {
String name = entry.getName();
if (name.endsWith(DOT_CLASS)) {
try {
byte[] bytes = ByteStreams.toByteArray(zis);
if (bytes != null) {
File file = new File(entry.getName());
entries.add(new ClassEntry(file, jarFile, jarFile, bytes));
}
} catch (Exception e) {
client.log(e, null);
continue;
}
}
entry = zis.getNextEntry();
}
} finally {
Closeables.close(fis, true);
}
} catch (IOException e) {
client.log(e, "Could not read jar file contents from %1$s", jarFile);
} finally {
try {
Closeables.close(zis, true);
} catch (IOException e) {
// cannot happen
}
}
} else if (classPathEntry.isDirectory()) {
//noinspection UnnecessaryLocalVariable
File binDir = classPathEntry;
List<File> classFiles = new ArrayList<File>();
addClassFiles(binDir, classFiles);
for (File file : classFiles) {
try {
byte[] bytes = client.readBytes(file);
entries.add(new ClassEntry(file, null, /* jarFile*/
binDir, bytes));
} catch (IOException e) {
client.log(e, null);
}
}
} else {
client.log(null, "Ignoring class path entry %1$s", classPathEntry);
}
}
}
use of java.util.zip.ZipInputStream in project android by JetBrains.
the class AndroidApkBuilder method collectDuplicateEntries.
@SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
private static void collectDuplicateEntries(@NotNull String rootFile, @NotNull Set<String> entries, @NotNull Set<String> result) throws IOException {
final JavaResourceFilter javaResourceFilter = new JavaResourceFilter();
FileInputStream fis = null;
ZipInputStream zis = null;
try {
fis = new FileInputStream(rootFile);
zis = new ZipInputStream(fis);
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
String name = entry.getName();
if (javaResourceFilter.checkEntry(name) && !entries.add(name)) {
result.add(name);
}
zis.closeEntry();
}
}
} finally {
if (zis != null) {
zis.close();
}
if (fis != null) {
fis.close();
}
}
}
use of java.util.zip.ZipInputStream in project graphhopper by graphhopper.
the class Unzipper method unzip.
/**
* @param progressListener updates not in percentage but the number of bytes already read.
*/
public void unzip(InputStream fromIs, File toFolder, ProgressListener progressListener) throws IOException {
if (!toFolder.exists())
toFolder.mkdirs();
long sumBytes = 0;
ZipInputStream zis = new ZipInputStream(fromIs);
try {
ZipEntry ze = zis.getNextEntry();
byte[] buffer = new byte[8 * 1024];
while (ze != null) {
if (ze.isDirectory()) {
new File(toFolder, ze.getName()).mkdir();
} else {
double factor = 1;
if (ze.getCompressedSize() > 0 && ze.getSize() > 0)
factor = (double) ze.getCompressedSize() / ze.getSize();
File newFile = new File(toFolder, ze.getName());
FileOutputStream fos = new FileOutputStream(newFile);
try {
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
sumBytes += len * factor;
if (progressListener != null)
progressListener.update(sumBytes);
}
} finally {
fos.close();
}
}
ze = zis.getNextEntry();
}
zis.closeEntry();
} finally {
zis.close();
}
}
Aggregations