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

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

Introduction

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

Prototype

public SevenZFile(final File filename) throws IOException 

Source Link

Document

Reads a file as unencrypted 7z archive

Usage

From source file:com.github.n_i_e.dirtreedb.SevenZipListerForFile.java

public SevenZipListerForFile(PathEntry basepath, File contentpath) throws IOException {
    super(basepath);
    sevenzfile = new SevenZFile(contentpath);
}

From source file:com.izforge.izpack.util.compress.SevenZArchiveInputStream.java

public SevenZArchiveInputStream(final File file) throws IOException {
    this.zFile = new SevenZFile(file);
}

From source file:es.ucm.fdi.util.archive.SevenZipFormat.java

public ArrayList<String> list(File source) throws IOException {
    assertIs7Zip(source);//from  www  .  j av a  2  s. co m

    SevenZFile zf = new SevenZFile(source);
    ArrayList<String> paths = new ArrayList<String>();

    for (SevenZArchiveEntry e : zf.getEntries()) {
        String name = FileUtils.toCanonicalPath(e.getName());
        if (e.isDirectory()) {
            continue;
        }

        paths.add(name);
    }
    return paths;
}

From source file:at.treedb.util.SevenZip.java

/**
 * Constructor/*from  w  ww  .j ava 2s . c  o m*/
 * 
 * @param file
 *            7-zip archive file
 * @param iface
 *            interface to display the extraction progress
 * @throws IOException
 */
public SevenZip(File file, DomainInstallationIface iface) throws Exception {
    sevenZFile = new SevenZFile(file);
    entryMap = new HashMap<String, ArchiveEntry>();
    // create a temporary file containing the extracted archive
    dumpFile = FileStorage.getInstance().createTempFile("sevenZip", ".dump");
    FileOutputStream writer = new FileOutputStream(dumpFile);
    byte[] readBuffer = new byte[READ_BUFFER];
    int offset = 0;
    if (iface != null) {
        iface.writeInternalMessage(InstallationProgress.Type.MSG,
                "Extracting installation package: '.' stands for " + READ_BUFFER + " MB data");
        iface.writeInternalMessage(null, "<br>");
    }
    int count = 0;
    int bytesWritten = 0;
    // extract and dump the files
    do {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (!entry.isDirectory()) {
            int readBytes = (int) entry.getSize();
            int size = readBytes;
            int index = 0;
            while (readBytes > 0) {
                int read = Math.min(readBytes, READ_BUFFER);
                bytesWritten += read;
                if (iface != null) {
                    iface.writeInternalMessage(null, "<b>.</b>");
                    ++count;
                    if (count == 80) {
                        iface.writeInternalMessage(null, "<br>");
                        count = 0;
                    }
                }
                sevenZFile.read(readBuffer, index, read);
                writer.write(readBuffer, 0, read);
                readBytes -= read;
            }
            entryMap.put(entry.getName().replace('\\', '/'), new ArchiveEntry(offset, size));
            offset += size;
        }
    } while (true);
    if (iface != null) {
        iface.writeInternalMessage(null, "<br>");
        iface.writeInternalMessage(InstallationProgress.Type.MSG,
                "Extraction finished: " + bytesWritten + " bytes written");
    }
    writer.close();
    dumpReader = new FileInputStream(dumpFile);
    channel = dumpReader.getChannel();
}

From source file:es.ucm.fdi.util.archive.SevenZipFormat.java

public void expand(File source, File destDir) throws IOException {
    assertIs7Zip(source);//from ww  w.ja  v  a 2 s .c  o  m

    try (SevenZFile zf = new SevenZFile(source)) {
        log.info("Extracting 7zip: " + source.getName());

        while (true) {
            SevenZArchiveEntry e = zf.getNextEntry();
            if (e == null)
                return;

            String name = FileUtils.toCanonicalPath(e.getName());
            log.info(" - processing 7zip entry: " + name + " - " + e.getSize());

            if (e.isDirectory()) {
                //log.debug("\tExtracting directory "+e.getName());
                File dir = new File(destDir, name);
                dir.mkdirs();
                continue;
            }

            //log.debug("\tExtracting file "+name);
            File outFile = new File(destDir, name);
            if (!outFile.getParentFile().exists()) {
                //log.warn("weird 7z: had to create parent: "+outFile.getParentFile());
                outFile.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(outFile)) {
                byte[] b = new byte[(int) e.getSize()];
                zf.read(b);
                fos.write(b);
            }
        }
    }
}

From source file:com.espringtran.compressor4j.processor.SevenZipProcessor.java

/**
 * Read from compressed file//from  w w w  . j a  va 2  s. c o m
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SevenZFile sevenZFile = new SevenZFile(new File(srcPath));
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = sevenZFile.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = sevenZFile.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = sevenZFile.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        sevenZFile.close();
        baos.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:at.treedb.backup.Import.java

/**
 * Read all archive entries.//w ww . j av  a  2  s . c  o m
 * 
 * @return archive entries
 * @throws IOException
 */
private HashMap<String, SevenZArchiveEntry> readEntries() throws IOException {
    HashMap<String, SevenZArchiveEntry> archiveMap = new HashMap<String, SevenZArchiveEntry>();
    sevenZFile = new SevenZFile(new File(archivePath));
    while (true) {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        if (entry.isDirectory()) {
            continue;
        }
        archiveMap.put(entry.getName(), entry);
    }
    sevenZFile.close();
    return archiveMap;
}

From source file:es.ucm.fdi.util.archive.SevenZipFormat.java

public boolean extractOne(File source, String path, File dest) throws IOException {
    assertIs7Zip(source);//ww w . j  a va  2  s .c  o  m

    try (SevenZFile zf = new SevenZFile(source)) {
        for (SevenZArchiveEntry e : zf.getEntries()) {
            String name = FileUtils.toCanonicalPath(e.getName());
            // System.err.println(" "+name+" =? "+path);
            if (!name.equals(path) || e.isDirectory()) {
                continue;
            }

            if (!dest.getParentFile().exists()) {
                //log.warn("weird 7z: had to create parent: "+outFile.getParentFile());
                dest.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(dest)) {
                byte[] b = new byte[(int) e.getSize()];
                zf.read(b);
                return true;
            }
        }
    }
    return false;
}

From source file:inventory.pl.services.BackupService.java

public void restore(String source, String outputFolder) {
    byte[] buffer = new byte[1024];

    try {/*from   w w w .  ja  v  a  2 s.co m*/

        //create output directory is not exists
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        //get the zip file content
        SevenZFile sevenZFile = new SevenZFile(new File(source));
        //get the zipped file list entry
        SevenZArchiveEntry ze = sevenZFile.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = sevenZFile.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = sevenZFile.getNextEntry();
        }

        sevenZFile.close();

        System.out.println("Done");

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:at.treedb.util.SevenZip.java

/**
 * Extracts some data (files and directories) from the archive without
 * extracting the whole archive.//w  ww  . j a  v a2s  .  co  m
 * 
 * @param archive
 *            7-zip archive
 * @param fileList
 *            file extraction list, a path with an ending '/' denotes a
 *            directory
 * @return file list as a map file name/file data
 * @throws IOException
 */
public static HashMap<String, byte[]> exctact(File archive, String... fileList) throws IOException {
    HashSet<String> fileSet = new HashSet<String>();
    ArrayList<String> dirList = new ArrayList<String>();
    for (String f : fileList) {
        if (!f.endsWith("/")) {
            fileSet.add(f);
        } else {
            dirList.add(f);
        }
    }
    HashMap<String, byte[]> resultMap = new HashMap<String, byte[]>();
    SevenZFile sevenZFile = new SevenZFile(archive);
    do {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        if (entry == null) {
            break;
        }
        // convert window path to unix style
        String name = entry.getName().replace('\\', '/');
        if (!entry.isDirectory()) {
            boolean storeFile = false;
            if (fileSet.contains(name)) {
                storeFile = true;
            } else {
                // search directories
                for (String s : dirList) {
                    if (name.startsWith(s)) {
                        storeFile = true;
                        break;
                    }
                }
            }
            // store the file
            if (storeFile) {
                int size = (int) entry.getSize();
                byte[] data = new byte[size];
                sevenZFile.read(data, 0, size);
                resultMap.put(name, data);
                // in this case we can finish the extraction loop
                if (dirList.isEmpty() && resultMap.size() == fileSet.size()) {
                    break;
                }
            }
        }
    } while (true);
    sevenZFile.close();
    return resultMap;
}