Example usage for org.apache.commons.compress.archivers.sevenz SevenZArchiveEntry getName

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

Introduction

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

Prototype

@Override
public String getName() 

Source Link

Document

Get this entry's name.

Usage

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

/**
 * Extracts some data (files and directories) from the archive without
 * extracting the whole archive./*from www.  jav a2  s . c  o 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;
}

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

public ArrayList<String> list(File source) throws IOException {
    assertIs7Zip(source);//from w ww.  j  av  a 2  s. c  om

    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:es.ucm.fdi.util.archive.SevenZipFormat.java

public boolean extractOne(File source, String path, File dest) throws IOException {
    assertIs7Zip(source);//from w  w w  .j  a v a2  s . c om

    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:es.ucm.fdi.util.archive.SevenZipFormat.java

public void expand(File source, File destDir) throws IOException {
    assertIs7Zip(source);/*from ww  w.  j a  va2s  . 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.github.n_i_e.dirtreedb.SevenZipListerForFile.java

@Override
protected PathEntry getNext() throws IOException {
    SevenZArchiveEntry z = sevenzfile.getNextEntry();
    if (z == null) {
        return null;
    }/*from www  . j ava  2s .  com*/
    int newtype = z.isDirectory() ? PathEntry.COMPRESSEDFOLDER : PathEntry.COMPRESSEDFILE;
    String s = z.getName();
    if (s == null) {
        s = AbstractCompressorLister.getBasename(getBasePath());
    }
    ;
    s = s.replace("\\", "/");
    if (z.isDirectory() && !s.endsWith("/")) {
        s = s + "/";
    }
    PathEntry next_entry = new PathEntry(getBasePath().getPath() + "/" + s, newtype);
    next_entry.setDateLastModified(z.getLastModifiedDate().getTime());
    next_entry.setStatus(PathEntry.DIRTY);
    next_entry.setSize(z.getSize());
    next_entry.setCompressedSize(z.getSize());
    if (isCsumRequested() && newtype == PathEntry.COMPRESSEDFILE) {
        next_entry.setCsum(new SevenZipInputStream());
    }
    if (next_entry.getSize() < 0) {
        next_entry.setSize(0);
    }
    if (next_entry.getCompressedSize() < 0) {
        next_entry.setCompressedSize(next_entry.getSize());
    }
    return next_entry;
}

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:de.tudarmstadt.ukp.dkpro.core.api.datasets.internal.actions.Explode.java

private void extract7z(ActionDescription aAction, Path aCachedFile, Path aTarget)
        throws IOException, RarException {
    // We always extract archives into a subfolder. Figure out the name of the folder.
    String base = getBase(aCachedFile.getFileName().toString());

    Map<String, Object> cfg = aAction.getConfiguration();
    int strip = cfg.containsKey("strip") ? (int) cfg.get("strip") : 0;

    AntFileFilter filter = new AntFileFilter(coerceToList(cfg.get("includes")),
            coerceToList(cfg.get("excludes")));

    try (SevenZFile archive = new SevenZFile(aCachedFile.toFile())) {
        SevenZArchiveEntry entry = archive.getNextEntry();
        while (entry != null) {
            String name = stripLeadingFolders(entry.getName(), strip);

            if (name == null) {
                // Stripped to null - nothing left to extract - continue;
                continue;
            }//from  www .  j a v  a 2  s . c om

            if (filter.accept(name)) {
                Path out = aTarget.resolve(base).resolve(name);
                if (entry.isDirectory()) {
                    Files.createDirectories(out);
                } else {
                    Files.createDirectories(out.getParent());
                    try (OutputStream os = Files.newOutputStream(out)) {
                        InputStream is = new SevenZEntryInputStream(archive, entry);
                        IOUtils.copyLarge(is, os);
                    }
                }
            }

            entry = archive.getNextEntry();
        }
    }
}

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

@Override
public ArchiveEntry getNextEntry() throws IOException {
    final SevenZArchiveEntry sevenZArchiveEntry = zFile.getNextEntry();

    currentEntry = sevenZArchiveEntry;/*  ww w. j  ava2 s .c o m*/
    currentBytesRead = 0;

    if (sevenZArchiveEntry != null) {
        return new ArchiveEntry() {
            @Override
            public String getName() {
                return sevenZArchiveEntry.getName();
            }

            @Override
            public long getSize() {
                return sevenZArchiveEntry.getSize();
            }

            @Override
            public boolean isDirectory() {
                return sevenZArchiveEntry.isDirectory();
            }

            @Override
            public Date getLastModifiedDate() {
                return sevenZArchiveEntry.getLastModifiedDate();
            }
        };
    }

    return null;
}

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

/**
 * Read from compressed file/*from  ww w . ja v a  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.//from w  w w.j  a  v  a2  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;
}