use of org.apache.commons.compress.archivers.ArchiveEntry in project xodus by JetBrains.
the class CompressBackupUtilTest method testFileArchived.
@Test
public void testFileArchived() throws Exception {
File src = new File(randName + ".txt");
FileWriter fw = new FileWriter(src);
fw.write("12345");
fw.close();
CompressBackupUtil.tar(src, dest);
Assert.assertTrue("No destination archive created", dest.exists());
TarArchiveInputStream tai = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest))));
ArchiveEntry entry = tai.getNextEntry();
Assert.assertNotNull("No entry found in destination archive", entry);
Assert.assertEquals("Entry has wrong size", 5, entry.getSize());
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.
the class EpubExtractor method getBookOverview.
@Override
public String getBookOverview(String path) {
String info = "";
try {
final File file = new File(path);
InputStream inputStream = new FileInputStream(file);
ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
ArchiveEntry nextEntry = null;
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
String name = nextEntry.getName().toLowerCase();
if (name.endsWith(".opf")) {
XmlPullParser xpp = XmlParser.buildPullParser();
xpp.setInput(zipInputStream, "utf-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if ("dc:description".equals(xpp.getName()) || "dcns:description".equals(xpp.getName())) {
info = xpp.nextText();
break;
}
}
if (eventType == XmlPullParser.END_TAG) {
if ("metadata".equals(xpp.getName())) {
break;
}
}
eventType = xpp.next();
}
}
}
zipInputStream.close();
inputStream.close();
} catch (Exception e) {
LOG.e(e);
}
return info;
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.
the class EpubExtractor method getBookMetaInformation.
@Override
public EbookMeta getBookMetaInformation(String path) {
final File file = new File(path);
try {
InputStream inputStream = new FileInputStream(file);
ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
ArchiveEntry nextEntry = null;
String title = null;
String author = null;
String subject = "";
String series = null;
String number = null;
String lang = null;
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
String name = nextEntry.getName().toLowerCase();
if (name.endsWith(".opf")) {
XmlPullParser xpp = XmlParser.buildPullParser();
xpp.setInput(zipInputStream, "utf-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if ("dc:title".equals(xpp.getName()) || "dcns:title".equals(xpp.getName())) {
title = xpp.nextText();
}
if ("dc:creator".equals(xpp.getName()) || "dcns:creator".equals(xpp.getName())) {
author = xpp.nextText();
}
if ("dc:subject".equals(xpp.getName()) || "dcns:subject".equals(xpp.getName())) {
subject = xpp.nextText() + "," + subject;
}
if (lang == null && ("dc:language".equals(xpp.getName()) || "dcns:language".equals(xpp.getName()))) {
lang = xpp.nextText();
}
if ("meta".equals(xpp.getName())) {
String nameAttr = xpp.getAttributeValue(null, "name");
if ("calibre:series".equals(nameAttr)) {
series = xpp.getAttributeValue(null, "content");
} else if ("calibre:series_index".equals(nameAttr)) {
number = xpp.getAttributeValue(null, "content");
if (number != null) {
number = number.replace(".0", "");
}
}
}
}
if (eventType == XmlPullParser.END_TAG) {
if ("metadata".equals(xpp.getName())) {
break;
}
}
eventType = xpp.next();
}
}
}
zipInputStream.close();
inputStream.close();
if (AppState.get().isFirstSurname) {
author = TxtUtils.replaceLastFirstName(author);
}
EbookMeta ebookMeta = new EbookMeta(title, author, series, subject.replaceAll(",$", ""));
try {
if (number != null) {
ebookMeta.setsIndex(Integer.parseInt(number));
}
} catch (Exception e) {
title = title + " [" + number + "]";
ebookMeta.setTitle(title);
LOG.d(e);
}
ebookMeta.setLang(lang);
// ebookMeta.setPagesCount((int) size / 1024);
return ebookMeta;
} catch (Exception e) {
return EbookMeta.Empty();
}
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.
the class ChangeSet method addAddition.
/**
* Adds an addition change.
*
* @param pChange
* the change which should result in an addition
*/
private void addAddition(final Change pChange) {
if (Change.TYPE_ADD != pChange.type() || pChange.getInput() == null) {
return;
}
if (!changes.isEmpty()) {
for (final Iterator<Change> it = changes.iterator(); it.hasNext(); ) {
final Change change = it.next();
if (change.type() == Change.TYPE_ADD && change.getEntry() != null) {
final ArchiveEntry entry = change.getEntry();
if (entry.equals(pChange.getEntry())) {
if (pChange.isReplaceMode()) {
it.remove();
changes.add(pChange);
return;
}
// do not add this change
return;
}
}
}
}
changes.add(pChange);
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.
the class ChangeSetPerformer method perform.
/**
* Performs all changes collected in this ChangeSet on the input entries and
* streams the result to the output stream.
*
* This method finishes the stream, no other entries should be added
* after that.
*
* @param entryIterator
* the entries to perform the changes on
* @param out
* the resulting OutputStream with all modifications
* @throws IOException
* if an read/write error occurs
* @return the results of this operation
*/
private ChangeSetResults perform(final ArchiveEntryIterator entryIterator, final ArchiveOutputStream out) throws IOException {
final ChangeSetResults results = new ChangeSetResults();
final Set<Change> workingSet = new LinkedHashSet<>(changes);
for (final Iterator<Change> it = workingSet.iterator(); it.hasNext(); ) {
final Change change = it.next();
if (change.type() == Change.TYPE_ADD && change.isReplaceMode()) {
copyStream(change.getInput(), out, change.getEntry());
it.remove();
results.addedFromChangeSet(change.getEntry().getName());
}
}
while (entryIterator.hasNext()) {
final ArchiveEntry entry = entryIterator.next();
boolean copy = true;
for (final Iterator<Change> it = workingSet.iterator(); it.hasNext(); ) {
final Change change = it.next();
final int type = change.type();
final String name = entry.getName();
if (type == Change.TYPE_DELETE && name != null) {
if (name.equals(change.targetFile())) {
copy = false;
it.remove();
results.deleted(name);
break;
}
} else if (type == Change.TYPE_DELETE_DIR && name != null) {
// don't combine ifs to make future extensions more easy
if (name.startsWith(change.targetFile() + "/")) {
// NOPMD
copy = false;
results.deleted(name);
break;
}
}
}
if (copy && !isDeletedLater(workingSet, entry) && !results.hasBeenAdded(entry.getName())) {
copyStream(entryIterator.getInputStream(), out, entry);
results.addedFromStream(entry.getName());
}
}
// Adds files which hasn't been added from the original and do not have replace mode on
for (final Iterator<Change> it = workingSet.iterator(); it.hasNext(); ) {
final Change change = it.next();
if (change.type() == Change.TYPE_ADD && !change.isReplaceMode() && !results.hasBeenAdded(change.getEntry().getName())) {
copyStream(change.getInput(), out, change.getEntry());
it.remove();
results.addedFromChangeSet(change.getEntry().getName());
}
}
out.finish();
return results;
}
Aggregations