use of org.apache.commons.compress.archivers.ArchiveEntry in project xwiki-platform by xwiki.
the class Package method Import.
/**
* Load this package in memory from an InputStream. It may be installed later using {@link #install(XWikiContext)}.
*
* @param file an InputStream of a zipped package file
* @param context current XWikiContext
* @return an empty string, useless.
* @throws IOException while reading the ZipFile
* @throws XWikiException when package content is broken
* @since 2.3M2
*/
public String Import(InputStream file, XWikiContext context) throws IOException, XWikiException {
ZipArchiveInputStream zis;
ArchiveEntry entry;
Document description = null;
try {
zis = new ZipArchiveInputStream(file, XAR_FILENAME_ENCODING, false);
List<XWikiDocument> docsToLoad = new LinkedList<XWikiDocument>();
/*
* Loop 1: Cycle through the zip input stream and load out all of the documents, when we find the
* package.xml file we put it aside to so that we only include documents which are in the file.
*/
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory() || (entry.getName().indexOf("META-INF") != -1)) {
// (we use that directory to put meta data such as LICENSE/NOTICE files.)
continue;
} else if (entry.getName().compareTo(DefaultPackageFileName) == 0) {
// The entry is the manifest (package.xml). Read this differently.
description = fromXml(new CloseShieldInputStream(zis));
} else {
XWikiDocument doc = null;
try {
doc = readFromXML(new CloseShieldInputStream(zis));
} catch (Throwable e) {
LOGGER.warn("Failed to parse document [{}] from XML during import, thus it will not be installed. " + "The error was: " + ExceptionUtils.getRootCauseMessage(e));
// It will be listed in the "failed documents" section after the import.
addToErrors(entry.getName().replaceAll("/", "."), context);
continue;
}
// if no filters throw exceptions, add it to the list to import.
try {
this.filter(doc, context);
docsToLoad.add(doc);
} catch (ExcludeDocumentException e) {
LOGGER.info("Skip the document '" + doc.getDocumentReference() + "'");
}
}
}
// Make sure a manifest was included in the package...
if (description == null) {
throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Could not find the package definition");
}
/*
* Loop 2: Cycle through the list of documents and if they are in the manifest then add them, otherwise log
* a warning and add them to the skipped list.
*/
for (XWikiDocument doc : docsToLoad) {
if (documentExistInPackageFile(doc.getFullName(), doc.getLanguage(), description)) {
this.add(doc, context);
} else {
LOGGER.warn("document " + doc.getDocumentReference() + " does not exist in package definition." + " It will not be installed.");
// It will be listed in the "skipped documents" section after the
// import.
addToSkipped(doc.getFullName(), context);
}
}
updateFileInfos(description);
} catch (DocumentException e) {
throw new PackageException(XWikiException.ERROR_XWIKI_UNKNOWN, "Error when reading the XML");
}
return "";
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.
the class EpubExtractor method proccessHypensApache.
public static void proccessHypensApache(String input, String output) throws Exception {
LOG.d("proccessHypens2", input, output);
FileInputStream inputStream = new FileInputStream(new File(input));
ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
ArchiveEntry nextEntry = null;
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(output)));
zos.setLevel(0);
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
if (TempHolder.get().loadingCancelled) {
break;
}
String name = nextEntry.getName();
String nameLow = name.toLowerCase();
if (!name.endsWith("container.xml") && (nameLow.endsWith("html") || nameLow.endsWith("htm") || nameLow.endsWith("xml"))) {
LOG.d("nextEntry HTML cancell", TempHolder.get().loadingCancelled, name);
ByteArrayOutputStream hStream = Fb2Extractor.generateHyphenFile(new InputStreamReader(zipInputStream));
Fb2Extractor.writeToZipNoClose(zos, name, new ByteArrayInputStream(hStream.toByteArray()));
} else {
LOG.d("nextEntry cancell", TempHolder.get().loadingCancelled, name);
Fb2Extractor.writeToZipNoClose(zos, name, zipInputStream);
}
}
zipInputStream.close();
inputStream.close();
zos.close();
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project LibreraReader by foobnix.
the class EpubExtractor method getBookCover.
@Override
public byte[] getBookCover(String path) {
byte[] cover = null;
try {
InputStream inputStream = new FileInputStream(new File(path));
ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
ArchiveEntry nextEntry = null;
String coverName = null;
String coverResource = null;
while (coverName == null && (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 ("meta".equals(xpp.getName()) && "cover".equals(xpp.getAttributeValue(null, "name"))) {
coverResource = xpp.getAttributeValue(null, "content");
}
if (coverResource != null && "item".equals(xpp.getName()) && coverResource.equals(xpp.getAttributeValue(null, "id"))) {
coverName = xpp.getAttributeValue(null, "href");
if (coverName != null && coverName.endsWith(".svg")) {
coverName = null;
}
break;
}
}
eventType = xpp.next();
}
}
}
if (coverName != null) {
zipInputStream.close();
inputStream.close();
inputStream = new FileInputStream(new File(path));
zipInputStream = new ZipArchiveInputStream(inputStream);
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
String name = nextEntry.getName();
if (name.contains(coverName)) {
cover = BaseExtractor.getEntryAsByte(zipInputStream);
break;
}
}
}
if (cover == null) {
zipInputStream.close();
inputStream.close();
inputStream = new FileInputStream(new File(path));
zipInputStream = new ZipArchiveInputStream(inputStream);
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
String name = nextEntry.getName().toLowerCase(Locale.US);
if (name.endsWith(".jpeg") || name.endsWith(".jpg") || name.endsWith(".png")) {
if (name.contains("cover")) {
cover = BaseExtractor.getEntryAsByte(zipInputStream);
break;
}
}
}
}
if (cover == null) {
zipInputStream.close();
inputStream.close();
inputStream = new FileInputStream(new File(path));
zipInputStream = new ZipArchiveInputStream(inputStream);
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
String name = nextEntry.getName().toLowerCase(Locale.US);
if (name.endsWith(".jpeg") || name.endsWith(".jpg") || name.endsWith(".png")) {
cover = BaseExtractor.getEntryAsByte(zipInputStream);
break;
}
}
}
zipInputStream.close();
inputStream.close();
} catch (Exception e) {
LOG.e(e);
}
return cover;
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project xodus by JetBrains.
the class CompressBackupUtilTest method testFolderArchived.
@Test
public void testFolderArchived() throws Exception {
File src = new File(randName);
src.mkdir();
FileWriter fw = new FileWriter(new File(src, "1.txt"));
fw.write("12345");
fw.close();
fw = new FileWriter(new File(src, "2.txt"));
fw.write("12");
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 entry1 = tai.getNextEntry();
ArchiveEntry entry2 = tai.getNextEntry();
if (entry1.getName().compareTo(entry2.getName()) > 0) {
// kinda sort them lol
ArchiveEntry tmp = entry1;
entry1 = entry2;
entry2 = tmp;
}
Assert.assertNotNull("No entry found in destination archive", entry1);
Assert.assertEquals("Entry has wrong size", 5, entry1.getSize());
System.out.println(entry1.getName());
Assert.assertEquals("Entry has wrong relative path", src.getName() + "/1.txt", entry1.getName());
System.out.println(entry2.getName());
Assert.assertEquals("Entry has wrong size", 2, entry2.getSize());
Assert.assertEquals("Entry has wrong relative path", src.getName() + "/2.txt", entry2.getName());
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project AozoraEpub3 by hmdev.
the class AozoraEpub3 method getTextInputStream.
/**
* 入力ファイルからStreamオープン
*
* @param srcFile
* @param ext
* @param imageInfoReader
* @param txtIdx テキストファイルのZip内の位置
* @return テキストファイルのストリーム (close()は呼び出し側ですること)
* @throws RarException
*/
public static InputStream getTextInputStream(File srcFile, String ext, ImageInfoReader imageInfoReader, String[] textEntryName, int txtIdx) throws IOException, RarException {
if ("txt".equals(ext)) {
return new FileInputStream(srcFile);
} else if ("zip".equals(ext) || "txtz".equals(ext)) {
// Zipなら最初のtxt
ZipArchiveInputStream zis = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), 65536), "MS932", false);
ArchiveEntry entry;
while ((entry = zis.getNextEntry()) != null) {
String entryName = entry.getName();
if (entryName.substring(entryName.lastIndexOf('.') + 1).equalsIgnoreCase("txt") && txtIdx-- == 0) {
if (imageInfoReader != null)
imageInfoReader.setArchiveTextEntry(entryName);
if (textEntryName != null)
textEntryName[0] = entryName;
return zis;
}
}
LogAppender.append("zip内にtxtファイルがありません: ");
LogAppender.println(srcFile.getName());
return null;
} else if ("rar".equals(ext)) {
// tempのtxtファイル作成
Archive archive = new Archive(srcFile);
try {
FileHeader fileHeader = archive.nextFileHeader();
while (fileHeader != null) {
if (!fileHeader.isDirectory()) {
String entryName = fileHeader.getFileNameW();
if (entryName.length() == 0)
entryName = fileHeader.getFileNameString();
entryName = entryName.replace('\\', '/');
if (entryName.substring(entryName.lastIndexOf('.') + 1).equalsIgnoreCase("txt") && txtIdx-- == 0) {
if (imageInfoReader != null)
imageInfoReader.setArchiveTextEntry(entryName);
if (textEntryName != null)
textEntryName[0] = entryName;
// tmpファイルにコピーして終了時に削除
File tmpFile = File.createTempFile("rarTmp", "txt");
tmpFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tmpFile);
InputStream is = archive.getInputStream(fileHeader);
try {
IOUtils.copy(is, fos);
} finally {
is.close();
fos.close();
}
return new BufferedInputStream(new FileInputStream(tmpFile), 65536);
}
}
fileHeader = archive.nextFileHeader();
}
} finally {
archive.close();
}
LogAppender.append("rar内にtxtファイルがありません: ");
LogAppender.println(srcFile.getName());
return null;
} else {
LogAppender.append("txt, zip, rar, txtz, cbz のみ変換可能です: ");
LogAppender.println(srcFile.getPath());
}
return null;
}
Aggregations