use of org.apache.commons.compress.archivers.zip.ZipArchiveInputStream in project gerrit by GerritCodeReview.
the class UploadArchiveIT method zipFormat.
@Test
public void zipFormat() throws Exception {
PushOneCommit.Result r = createChange();
String abbreviated = r.getCommit().abbreviate(8).name();
String c = command(r, abbreviated);
InputStream out = adminSshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c));
// Wrap with PacketLineIn to read ACK bytes from output stream
PacketLineIn in = new PacketLineIn(out);
String tmp = in.readString();
assertThat(tmp).isEqualTo("ACK");
tmp = in.readString();
// Skip length (4 bytes) + 1 byte
// to position the output stream to the raw zip stream
byte[] buffer = new byte[5];
IO.readFully(out, buffer, 0, 5);
Set<String> entryNames = new TreeSet<>();
try (ZipArchiveInputStream zip = new ZipArchiveInputStream(out)) {
ZipArchiveEntry zipEntry = zip.getNextZipEntry();
while (zipEntry != null) {
String name = zipEntry.getName();
entryNames.add(name);
zipEntry = zip.getNextZipEntry();
}
}
assertThat(entryNames.size()).isEqualTo(1);
assertThat(Iterables.getOnlyElement(entryNames)).isEqualTo(String.format("%s/%s", abbreviated, PushOneCommit.FILE_NAME));
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveInputStream in project uPortal by Jasig.
the class JaxbPortalDataHandlerService method importDataArchive.
protected void importDataArchive(Resource archive, InputStream resourceStream, BatchImportOptions options) {
BufferedInputStream bufferedResourceStream = null;
try {
//Make sure the stream is buffered
if (resourceStream instanceof BufferedInputStream) {
bufferedResourceStream = (BufferedInputStream) resourceStream;
} else {
bufferedResourceStream = new BufferedInputStream(resourceStream);
}
//Buffer up to 100MB, bad things will happen if we bust this buffer.
//TODO see if there is a buffered stream that will write to a file once the buffer fills up
bufferedResourceStream.mark(100 * 1024 * 1024);
final MediaType type = getMediaType(bufferedResourceStream, archive.getFilename());
if (MT_JAVA_ARCHIVE.equals(type)) {
final ArchiveInputStream archiveStream = new JarArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MediaType.APPLICATION_ZIP.equals(type)) {
final ArchiveInputStream archiveStream = new ZipArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MT_CPIO.equals(type)) {
final ArchiveInputStream archiveStream = new CpioArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MT_AR.equals(type)) {
final ArchiveInputStream archiveStream = new ArArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MT_TAR.equals(type)) {
final ArchiveInputStream archiveStream = new TarArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MT_BZIP2.equals(type)) {
final CompressorInputStream compressedStream = new BZip2CompressorInputStream(bufferedResourceStream);
importDataArchive(archive, compressedStream, options);
} else if (MT_GZIP.equals(type)) {
final CompressorInputStream compressedStream = new GzipCompressorInputStream(bufferedResourceStream);
importDataArchive(archive, compressedStream, options);
} else if (MT_PACK200.equals(type)) {
final CompressorInputStream compressedStream = new Pack200CompressorInputStream(bufferedResourceStream);
importDataArchive(archive, compressedStream, options);
} else if (MT_XZ.equals(type)) {
final CompressorInputStream compressedStream = new XZCompressorInputStream(bufferedResourceStream);
importDataArchive(archive, compressedStream, options);
} else {
throw new RuntimeException("Unrecognized archive media type: " + type);
}
} catch (IOException e) {
throw new RuntimeException("Could not load InputStream for resource: " + archive, e);
} finally {
IOUtils.closeQuietly(bufferedResourceStream);
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveInputStream in project stanbol by apache.
the class ConfigUtils method getArchiveInputStream.
public static ArchiveInputStream getArchiveInputStream(String solrArchiveName, InputStream is) throws IOException {
String archiveFormat;
String solrArchiveExtension = FilenameUtils.getExtension(solrArchiveName);
if (solrArchiveExtension == null || solrArchiveExtension.isEmpty()) {
// assume that the archiveExtension was parsed
archiveFormat = solrArchiveName;
} else {
archiveFormat = SUPPORTED_SOLR_ARCHIVE_FORMAT.get(solrArchiveExtension);
}
ArchiveInputStream ais;
if ("zip".equals(archiveFormat)) {
ais = new ZipArchiveInputStream(is);
} else {
if ("gz".equals(archiveFormat)) {
is = new GZIPInputStream(is);
} else if ("bz2".equals(archiveFormat)) {
is = new BZip2CompressorInputStream(is);
} else {
throw new IllegalStateException("Unsupported compression format " + archiveFormat + "!. " + "Please report this to stanbol-dev mailing list!");
}
ais = new TarArchiveInputStream(is);
}
return ais;
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveInputStream in project baker-android by bakerframework.
the class UnzipperTask method extract.
private void extract(final String inputFile, final String outputDir) throws IOException {
FileInputStream fileInputStream = null;
ZipArchiveInputStream zipArchiveInputStream = null;
FileOutputStream fileOutputStream = null;
try {
Log.d(this.getClass().getName(), "Will extract " + inputFile + " to " + outputDir);
byte[] buffer = new byte[8192];
fileInputStream = new FileInputStream(inputFile);
// We use null as encoding.
zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream, null, true);
ArchiveEntry entry;
while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
Log.d(this.getClass().getName(), "Extracting entry " + entry.getName());
File file = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
} else {
file.getParentFile().mkdirs();
fileOutputStream = new FileOutputStream(file);
int bytesRead;
while ((bytesRead = zipArchiveInputStream.read(buffer, 0, buffer.length)) != -1) fileOutputStream.write(buffer, 0, bytesRead);
fileOutputStream.close();
fileOutputStream = null;
}
}
if (this.isDeleteZipFile()) {
// Delete the zip file
File zipFile = new File(inputFile);
zipFile.delete();
}
} finally {
try {
zipArchiveInputStream.close();
fileInputStream.close();
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (NullPointerException ex) {
Log.e(this.getClass().getName(), "Error closing the file streams.", ex);
} catch (IOException ex) {
Log.e(this.getClass().getName(), "Error closing the file streams.", ex);
}
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveInputStream 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