Search in sources :

Example 6 with ZipArchiveInputStream

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));
}
Also used : PacketLineIn(org.eclipse.jgit.transport.PacketLineIn) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TreeSet(java.util.TreeSet) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 7 with ZipArchiveInputStream

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);
    }
}
Also used : JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) CompressorInputStream(org.apache.commons.compress.compressors.CompressorInputStream) XZCompressorInputStream(org.apache.commons.compress.compressors.xz.XZCompressorInputStream) Pack200CompressorInputStream(org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) IOException(java.io.IOException) Pack200CompressorInputStream(org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) CpioArchiveInputStream(org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) CpioArchiveInputStream(org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream) XZCompressorInputStream(org.apache.commons.compress.compressors.xz.XZCompressorInputStream)

Example 8 with ZipArchiveInputStream

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)

Example 9 with ZipArchiveInputStream

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);
        }
    }
}
Also used : ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 10 with ZipArchiveInputStream

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;
}
Also used : Archive(com.github.junrar.Archive) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) FileHeader(com.github.junrar.rarfile.FileHeader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)15 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)8 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)8 BufferedInputStream (java.io.BufferedInputStream)7 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 File (java.io.File)4 Archive (com.github.junrar.Archive)3 FileHeader (com.github.junrar.rarfile.FileHeader)3 FileOutputStream (java.io.FileOutputStream)3 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)3 ImageInfo (com.github.hmdev.info.ImageInfo)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URL (java.net.URL)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)2 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)2 ChapterInfo (com.github.hmdev.info.ChapterInfo)1 ChapterLineInfo (com.github.hmdev.info.ChapterLineInfo)1