Example usage for org.apache.commons.compress.archivers.sevenz SevenZFile read

List of usage examples for org.apache.commons.compress.archivers.sevenz SevenZFile read

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.sevenz SevenZFile read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a byte of data.

Usage

From source file:org.apache.ant.compress.resources.SevenZResource.java

/**
 * Return an InputStream for reading the contents of this Resource.
 * @return an InputStream object./*from w ww.  java 2  s . co  m*/
 * @throws IOException if the sevenz file cannot be opened,
 *         or the entry cannot be read.
 */
public InputStream getInputStream() throws IOException {
    if (isReference()) {
        return ((Resource) getCheckedRef()).getInputStream();
    }
    File f = getSevenZFile();
    if (f == null) {
        return super.getInputStream();
    }

    final SevenZFile z = new SevenZFile(f);
    SevenZArchiveEntry ze = z.getNextEntry();
    while (ze != null) {
        if (ze.getName().equals(getName())) {
            return new InputStream() {
                public int read() throws IOException {
                    return z.read();
                }

                public int read(byte[] b) throws IOException {
                    return z.read(b);
                }

                public void close() throws IOException {
                    z.close();
                }

                protected void finalize() throws Throwable {
                    try {
                        close();
                    } finally {
                        super.finalize();
                    }
                }
            };
        }
        ze = z.getNextEntry();
    }
    z.close();
    throw new BuildException("no entry " + getName() + " in " + getArchive());
}

From source file:org.apache.ant.compress.taskdefs.Un7z.java

protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
    if (!srcF.exists()) {
        throw new BuildException("Unable to expand " + srcF + " as the file does not exist", getLocation());
    }// ww w . j a v a 2  s.  c o m
    log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
    FileNameMapper mapper = getMapper();
    SevenZFile outer = null;
    try {
        final SevenZFile zf = outer = new SevenZFile(srcF);
        boolean empty = true;
        SevenZArchiveEntry ze = zf.getNextEntry();
        while (ze != null) {
            empty = false;
            /* TODO implement canReadEntryData in CC
            if (getSkipUnreadableEntries() && !zf.canReadEntryData(ze)) {
            log(Messages.skippedIsUnreadable(ze));
            continue;
            }
            */
            log("extracting " + ze.getName(), Project.MSG_DEBUG);
            InputStream is = null;
            try {
                extractFile(fileUtils, srcF, dir, is = new InputStream() {
                    public int read() throws IOException {
                        return zf.read();
                    }

                    public int read(byte[] b) throws IOException {
                        return zf.read(b);
                    }
                }, ze.getName(), ze.getLastModifiedDate(), ze.isDirectory(), mapper);
            } finally {
                FileUtils.close(is);
            }
            ze = zf.getNextEntry();
        }
        if (empty && getFailOnEmptyArchive()) {
            throw new BuildException("archive '" + srcF + "' is empty");
        }
        log("expand complete", Project.MSG_VERBOSE);
    } catch (IOException ioe) {
        throw new BuildException("Error while expanding " + srcF.getPath() + "\n" + ioe.toString(), ioe);
    } finally {
        if (outer != null) {
            try {
                outer.close();
            } catch (IOException ex) {
                // swallow
            }
        }
    }
}

From source file:org.apache.marmotta.loader.core.MarmottaLoader.java

public void loadArchive(File archive, LoaderHandler handler, RDFFormat format)
        throws RDFParseException, IOException, ArchiveException {
    log.info("loading files in archive {} ...", archive);

    if (archive.exists() && archive.canRead()) {

        if (archive.getName().endsWith("7z")) {
            log.info("auto-detected archive format: 7Z");

            final SevenZFile sevenZFile = new SevenZFile(archive);

            try {
                SevenZArchiveEntry entry;
                while ((entry = sevenZFile.getNextEntry()) != null) {

                    if (!entry.isDirectory()) {
                        log.info("loading entry {} ...", entry.getName());

                        // detect the file format
                        RDFFormat detectedFormat = Rio.getParserFormatForFileName(entry.getName());
                        if (format == null) {
                            if (detectedFormat != null) {
                                log.info("auto-detected entry format: {}", detectedFormat.getName());
                                format = detectedFormat;
                            } else {
                                throw new RDFParseException(
                                        "could not detect input format of entry " + entry.getName());
                            }/*from   w ww .ja  v  a  2 s .  c om*/
                        } else {
                            if (detectedFormat != null && !format.equals(detectedFormat)) {
                                log.warn("user-specified entry format ({}) overrides auto-detected format ({})",
                                        format.getName(), detectedFormat.getName());
                            } else {
                                log.info("user-specified entry format: {}", format.getName());
                            }
                        }

                        load(new InputStream() {
                            @Override
                            public int read() throws IOException {
                                return sevenZFile.read();
                            }

                            @Override
                            public int read(byte[] b) throws IOException {
                                return sevenZFile.read(b);
                            }

                            @Override
                            public int read(byte[] b, int off, int len) throws IOException {
                                return sevenZFile.read(b, off, len);
                            }
                        }, handler, format);
                    }
                }
            } finally {
                sevenZFile.close();
            }

        } else {
            InputStream in;

            String archiveCompression = detectCompression(archive);
            InputStream fin = new BufferedInputStream(new FileInputStream(archive));
            if (archiveCompression != null) {
                if (CompressorStreamFactory.GZIP.equalsIgnoreCase(archiveCompression)) {
                    log.info("auto-detected archive compression: GZIP");
                    in = new GzipCompressorInputStream(fin, true);
                } else if (CompressorStreamFactory.BZIP2.equalsIgnoreCase(archiveCompression)) {
                    log.info("auto-detected archive compression: BZIP2");
                    in = new BZip2CompressorInputStream(fin, true);
                } else if (CompressorStreamFactory.XZ.equalsIgnoreCase(archiveCompression)) {
                    log.info("auto-detected archive compression: XZ");
                    in = new XZCompressorInputStream(fin, true);
                } else {
                    in = fin;
                }
            } else {
                in = fin;
            }

            ArchiveInputStream zipStream = new ArchiveStreamFactory()
                    .createArchiveInputStream(new BufferedInputStream(in));
            logArchiveType(zipStream);

            ArchiveEntry entry;
            while ((entry = zipStream.getNextEntry()) != null) {

                if (!entry.isDirectory()) {
                    log.info("loading entry {} ...", entry.getName());

                    // detect the file format
                    RDFFormat detectedFormat = Rio.getParserFormatForFileName(entry.getName());
                    if (format == null) {
                        if (detectedFormat != null) {
                            log.info("auto-detected entry format: {}", detectedFormat.getName());
                            format = detectedFormat;
                        } else {
                            throw new RDFParseException(
                                    "could not detect input format of entry " + entry.getName());
                        }
                    } else {
                        if (detectedFormat != null && !format.equals(detectedFormat)) {
                            log.warn("user-specified entry format ({}) overrides auto-detected format ({})",
                                    format.getName(), detectedFormat.getName());
                        } else {
                            log.info("user-specified entry format: {}", format.getName());
                        }
                    }

                    load(zipStream, handler, format);
                }
            }
        }

    } else {
        throw new RDFParseException(
                "could not load files from archive " + archive + ": it does not exist or is not readable");
    }

}

From source file:org.thelq.stackexchange.dbimport.analyze.ColumnAnalyzer.java

public static void main(String[] args) throws IOException, XMLStreamException {
    String archivePath = "C:\\Users\\Leon\\Downloads\\Stack Exchange Data Dump - Jun 2013\\Content\\android.stackexchange.com.7z";
    final SevenZFile archive = new SevenZFile(new File(archivePath));
    InputStream archiveWrappedInputStream = new InputStream() {
        @Override/*from w w w . jav  a 2 s  .c o m*/
        public int read() throws IOException {
            return archive.read();
        }

        @Override
        public int read(byte[] b) throws IOException {
            return archive.read(b);
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            return archive.read(b, off, len);
        }
    };
    for (SevenZArchiveEntry curEntry = archive.getNextEntry(); (curEntry = archive.getNextEntry()) != null;)
        if (curEntry.getName().endsWith(".xml"))
            analyze(archivePath + "\\" + curEntry.getName(), archiveWrappedInputStream);
}

From source file:org.thelq.stackexchange.dbimport.sources.ArchiveDumpEntry.java

public InputStream getInput() {
    if (input != null)
        throw new RuntimeException("Already generated an InputStream");

    try {/*w  w w.jav a  2  s .c om*/
        final SevenZFile file7 = new SevenZFile(file);
        //Advance archive until we find the correct ArchiveEntry
        SevenZArchiveEntry curEntry;
        while ((curEntry = file7.getNextEntry()) != null) {
            if (!curEntry.getName().equals(name))
                continue;
            //Found, return a wrapped InputStream
            return new InputStream() {
                @Override
                public int read() throws IOException {
                    return file7.read();
                }
            };
        }
    } catch (IOException ex) {
        throw new RuntimeException("Cannot open archive entry", ex);
    }
    //Didn't find anything
    throw new RuntimeException("Could not find file " + name + " in archive " + file.getAbsolutePath());
}