use of org.apache.commons.compress.archivers.sevenz.SevenZFile in project structr by structr.
the class UnarchiveCommand method unarchive.
private void unarchive(final SecurityContext securityContext, final File file, final String parentFolderId) throws ArchiveException, IOException, FrameworkException {
final App app = StructrApp.getInstance(securityContext);
final InputStream is;
Folder existingParentFolder = null;
final String fileName = file.getName();
try (final Tx tx = app.tx()) {
// search for existing parent folder
existingParentFolder = app.get(Folder.class, parentFolderId);
String parentFolderName = null;
String msgString = "Unarchiving file {}";
if (existingParentFolder != null) {
parentFolderName = existingParentFolder.getName();
msgString += " into existing folder {}.";
}
logger.info(msgString, new Object[] { fileName, parentFolderName });
is = file.getInputStream();
tx.success();
if (is == null) {
getWebSocket().send(MessageBuilder.status().code(400).message("Could not get input stream from file ".concat(fileName)).build(), true);
return;
}
tx.success();
}
final BufferedInputStream bufferedIs = new BufferedInputStream(is);
switch(ArchiveStreamFactory.detect(bufferedIs)) {
// 7z doesn't support streaming
case ArchiveStreamFactory.SEVEN_Z:
{
int overallCount = 0;
logger.info("7-Zip archive format detected");
try (final Tx outertx = app.tx()) {
SevenZFile sevenZFile = new SevenZFile(file.getFileOnDisk());
SevenZArchiveEntry sevenZEntry = sevenZFile.getNextEntry();
while (sevenZEntry != null) {
try (final Tx tx = app.tx(true, true, false)) {
int count = 0;
while (sevenZEntry != null && count++ < 50) {
final String entryPath = "/" + PathHelper.clean(sevenZEntry.getName());
logger.info("Entry path: {}", entryPath);
if (sevenZEntry.isDirectory()) {
handleDirectory(securityContext, existingParentFolder, entryPath);
} else {
byte[] buf = new byte[(int) sevenZEntry.getSize()];
sevenZFile.read(buf, 0, buf.length);
try (final ByteArrayInputStream in = new ByteArrayInputStream(buf)) {
handleFile(securityContext, in, existingParentFolder, entryPath);
}
}
sevenZEntry = sevenZFile.getNextEntry();
overallCount++;
}
logger.info("Committing transaction after {} entries.", overallCount);
tx.success();
}
}
logger.info("Unarchived {} files.", overallCount);
outertx.success();
}
break;
}
// ZIP needs special treatment to support "unsupported feature data descriptor"
case ArchiveStreamFactory.ZIP:
{
logger.info("Zip archive format detected");
try (final ZipArchiveInputStream in = new ZipArchiveInputStream(bufferedIs, null, false, true)) {
handleArchiveInputStream(in, app, securityContext, existingParentFolder);
}
break;
}
default:
{
logger.info("Default archive format detected");
try (final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(bufferedIs)) {
handleArchiveInputStream(in, app, securityContext, existingParentFolder);
}
}
}
getWebSocket().send(MessageBuilder.finished().callback(callback).data("success", true).data("filename", fileName).build(), true);
}
use of org.apache.commons.compress.archivers.sevenz.SevenZFile in project hutool by looly.
the class SevenZExtractor method extractInternal.
/**
* 释放(解压)到指定目录
*
* @param targetDir 目标目录
* @param filter 解压文件过滤器,用于指定需要释放的文件,null表示不过滤。当{@link Filter#accept(Object)}为true时释放。
* @throws IOException IO异常
*/
private void extractInternal(File targetDir, Filter<ArchiveEntry> filter) throws IOException {
Assert.isTrue(null != targetDir && ((false == targetDir.exists()) || targetDir.isDirectory()), "target must be dir.");
final SevenZFile sevenZFile = this.sevenZFile;
SevenZArchiveEntry entry;
File outItemFile;
while (null != (entry = this.sevenZFile.getNextEntry())) {
if (null != filter && false == filter.accept(entry)) {
continue;
}
outItemFile = FileUtil.file(targetDir, entry.getName());
if (entry.isDirectory()) {
// 创建对应目录
// noinspection ResultOfMethodCallIgnored
outItemFile.mkdirs();
} else if (entry.hasStream()) {
// 读取entry对应数据流
FileUtil.writeFromStream(new Seven7EntryInputStream(sevenZFile, entry), outItemFile);
} else {
// 无数据流的文件创建为空文件
FileUtil.touch(outItemFile);
}
}
}
use of org.apache.commons.compress.archivers.sevenz.SevenZFile in project tika by apache.
the class PackageParser method parse.
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
//lazily load the MediaTypeRegistry at parse time
//only want to call getDefaultConfig() once, and can't
//load statically because of the ForkParser
TikaConfig config = context.get(TikaConfig.class);
MediaTypeRegistry mediaTypeRegistry = null;
if (config != null) {
mediaTypeRegistry = config.getMediaTypeRegistry();
} else {
if (bufferedMediaTypeRegistry == null) {
//buffer this for next time.
synchronized (lock) {
//now that we're locked, check again
if (bufferedMediaTypeRegistry == null) {
bufferedMediaTypeRegistry = TikaConfig.getDefaultConfig().getMediaTypeRegistry();
}
}
}
mediaTypeRegistry = bufferedMediaTypeRegistry;
}
// Ensure that the stream supports the mark feature
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
TemporaryResources tmp = new TemporaryResources();
ArchiveInputStream ais = null;
try {
ArchiveStreamFactory factory = context.get(ArchiveStreamFactory.class, new ArchiveStreamFactory());
// At the end we want to close the archive stream to release
// any associated resources, but the underlying document stream
// should not be closed
ais = factory.createArchiveInputStream(new CloseShieldInputStream(stream));
} catch (StreamingNotSupportedException sne) {
// Most archive formats work on streams, but a few need files
if (sne.getFormat().equals(ArchiveStreamFactory.SEVEN_Z)) {
// Rework as a file, and wrap
stream.reset();
TikaInputStream tstream = TikaInputStream.get(stream, tmp);
// Seven Zip suports passwords, was one given?
String password = null;
PasswordProvider provider = context.get(PasswordProvider.class);
if (provider != null) {
password = provider.getPassword(metadata);
}
SevenZFile sevenz;
if (password == null) {
sevenz = new SevenZFile(tstream.getFile());
} else {
sevenz = new SevenZFile(tstream.getFile(), password.getBytes("UnicodeLittleUnmarked"));
}
// Pending a fix for COMPRESS-269 / TIKA-1525, this bit is a little nasty
ais = new SevenZWrapper(sevenz);
} else {
tmp.close();
throw new TikaException("Unknown non-streaming format " + sne.getFormat(), sne);
}
} catch (ArchiveException e) {
tmp.close();
throw new TikaException("Unable to unpack document stream", e);
}
updateMediaType(ais, mediaTypeRegistry, metadata);
// Use the delegate parser to parse the contained document
EmbeddedDocumentExtractor extractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
try {
ArchiveEntry entry = ais.getNextEntry();
while (entry != null) {
if (!entry.isDirectory()) {
parseEntry(ais, entry, extractor, metadata, xhtml);
}
entry = ais.getNextEntry();
}
} catch (UnsupportedZipFeatureException zfe) {
// If it's an encrypted document of unknown password, report as such
if (zfe.getFeature() == Feature.ENCRYPTION) {
throw new EncryptedDocumentException(zfe);
}
// Otherwise throw the exception
throw new TikaException("UnsupportedZipFeature", zfe);
} catch (PasswordRequiredException pre) {
throw new EncryptedDocumentException(pre);
} finally {
ais.close();
tmp.close();
}
xhtml.endDocument();
}
Aggregations