Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getSize

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getSize

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getSize.

Prototype

public long getSize() 

Source Link

Document

Get this entry's file size.

Usage

From source file:org.vafer.jdeb.mapping.LsMapper.java

public TarArchiveEntry map(final TarArchiveEntry pEntry) {

    final TarArchiveEntry entry = mapping.get(pEntry.getName());

    if (entry != null) {

        final TarArchiveEntry newEntry = new TarArchiveEntry(entry.getName(), true);
        newEntry.setUserId(entry.getUserId());
        newEntry.setGroupId(entry.getGroupId());
        newEntry.setUserName(entry.getUserName());
        newEntry.setGroupName(entry.getGroupName());
        newEntry.setMode(entry.getMode());
        newEntry.setSize(entry.getSize());

        return newEntry;
    }/*from ww w.java 2  s  .c o  m*/

    return pEntry;
}

From source file:org.vafer.jdeb.mapping.PermMapper.java

public TarArchiveEntry map(final TarArchiveEntry entry) {
    final String name = entry.getName();

    final TarArchiveEntry newEntry = new TarArchiveEntry(prefix + '/' + Utils.stripPath(strip, name), true);

    // Set ownership
    if (uid > -1) {
        newEntry.setUserId(uid);/*from  w w w . ja v  a 2  s.  c  o  m*/
    } else {
        newEntry.setUserId(entry.getUserId());
    }
    if (gid > -1) {
        newEntry.setGroupId(gid);
    } else {
        newEntry.setGroupId(entry.getGroupId());
    }
    if (user != null) {
        newEntry.setUserName(user);
    } else {
        newEntry.setUserName(entry.getUserName());
    }
    if (group != null) {
        newEntry.setGroupName(group);
    } else {
        newEntry.setGroupName(entry.getGroupName());
    }

    // Set permissions
    if (newEntry.isDirectory()) {
        if (dirMode > -1) {
            newEntry.setMode(dirMode);
        } else {
            newEntry.setMode(entry.getMode());
        }
    } else {
        if (fileMode > -1) {
            newEntry.setMode(fileMode);
        } else {
            newEntry.setMode(entry.getMode());

        }
    }

    newEntry.setSize(entry.getSize());

    return newEntry;
}

From source file:org.vafer.jdeb.producers.DataProducerArchive.java

public void produce(final DataConsumer pReceiver) throws IOException {

    InputStream is = new BufferedInputStream(new FileInputStream(archive));

    CompressorInputStream compressorInputStream = null;

    try {//from ww w .  j a v a 2s.c  o m
        compressorInputStream = new CompressorStreamFactory().createCompressorInputStream(is);
    } catch (CompressorException e) {
        // expected if the input file is a zip archive
    }

    if (compressorInputStream != null) {
        is = new BufferedInputStream(compressorInputStream);
    }

    ArchiveInputStream archiveInputStream = null;

    try {
        archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream(is);
    } catch (ArchiveException e) {
        throw new IOException("Unsupported archive format: " + archive, e);
    }

    EntryConverter converter = null;

    if (archiveInputStream instanceof TarArchiveInputStream) {

        converter = new EntryConverter() {
            public TarArchiveEntry convert(ArchiveEntry entry) {
                TarArchiveEntry src = (TarArchiveEntry) entry;
                TarArchiveEntry dst = new TarArchiveEntry(src.getName(), true);

                dst.setSize(src.getSize());
                dst.setGroupName(src.getGroupName());
                dst.setGroupId(src.getGroupId());
                dst.setUserId(src.getUserId());
                dst.setMode(src.getMode());
                dst.setModTime(src.getModTime());

                return dst;
            }
        };

    } else if (archiveInputStream instanceof ZipArchiveInputStream) {

        converter = new EntryConverter() {
            public TarArchiveEntry convert(ArchiveEntry entry) {
                ZipArchiveEntry src = (ZipArchiveEntry) entry;
                TarArchiveEntry dst = new TarArchiveEntry(src.getName(), true);

                dst.setSize(src.getSize());
                dst.setMode(src.getUnixMode());
                dst.setModTime(src.getTime());

                return dst;
            }
        };

    } else {
        throw new IOException("Unsupported archive format: " + archive);
    }

    try {
        while (true) {

            ArchiveEntry archiveEntry = archiveInputStream.getNextEntry();

            if (archiveEntry == null) {
                break;
            }

            if (!isIncluded(archiveEntry.getName())) {
                continue;
            }

            TarArchiveEntry entry = converter.convert(archiveEntry);

            entry = map(entry);

            if (entry.isDirectory()) {
                pReceiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(),
                        entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(),
                        entry.getSize());
                continue;
            }
            pReceiver.onEachFile(archiveInputStream, entry.getName(), entry.getLinkName(), entry.getUserName(),
                    entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(),
                    entry.getSize());
        }

    } finally {
        if (archiveInputStream != null) {
            archiveInputStream.close();
        }
    }
}

From source file:org.vafer.jdeb.producers.DataProducerDirectory.java

public void produce(final DataConsumer pReceiver) throws IOException {

    scanner.scan();/*from w w w . j a  v  a  2s .co  m*/

    final File baseDir = scanner.getBasedir();

    for (String dir : scanner.getIncludedDirectories()) {
        final File file = new File(baseDir, dir);
        String dirname = getFilename(baseDir, file);

        if ("".equals(dirname)) {
            continue;
        }

        if (!isIncluded(dirname)) {
            continue;
        }

        if ('/' != File.separatorChar) {
            dirname = dirname.replace(File.separatorChar, '/');
        }

        if (!dirname.endsWith("/")) {
            dirname += "/";
        }

        TarArchiveEntry entry = new TarArchiveEntry(dirname, true);
        entry.setUserId(0);
        entry.setUserName("root");
        entry.setGroupId(0);
        entry.setGroupName("root");
        entry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);

        entry = map(entry);

        entry.setSize(0);

        pReceiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(),
                entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
    }

    for (String f : scanner.getIncludedFiles()) {
        final File file = new File(baseDir, f);
        String filename = getFilename(baseDir, file);

        if (!isIncluded(filename)) {
            continue;
        }

        if ('/' != File.separatorChar) {
            filename = filename.replace(File.separatorChar, '/');
        }

        TarArchiveEntry entry = new TarArchiveEntry(filename, true);
        entry.setUserId(0);
        entry.setUserName("root");
        entry.setGroupId(0);
        entry.setGroupName("root");
        entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);

        entry = map(entry);

        entry.setSize(file.length());

        final InputStream inputStream = new FileInputStream(file);
        try {
            pReceiver.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(),
                    entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(),
                    entry.getSize());
        } finally {
            inputStream.close();
        }
    }
}

From source file:org.vafer.jdeb.producers.DataProducerFile.java

public void produce(final DataConsumer pReceiver) throws IOException {
    String fileName;//from  ww w . ja  va2  s . c  o  m
    if (destinationName != null && destinationName.trim().length() > 0) {
        fileName = destinationName.trim();
    } else {
        fileName = file.getName();
    }

    TarArchiveEntry entry = new TarArchiveEntry(fileName, true);
    entry.setUserId(0);
    entry.setUserName("root");
    entry.setGroupId(0);
    entry.setGroupName("root");
    entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);

    entry = map(entry);

    entry.setSize(file.length());

    final InputStream inputStream = new FileInputStream(file);
    try {
        pReceiver.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(),
                entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
    } finally {
        inputStream.close();
    }

}

From source file:org.vafer.jdeb.producers.DataProducerPathTemplate.java

public void produce(DataConsumer pReceiver) throws IOException {
    for (String literalPath : literalPaths) {
        TarArchiveEntry entry = new TarArchiveEntry(literalPath, true);
        entry.setUserId(0);/*w  ww .j  a  va2 s  .c  o m*/
        entry.setUserName("root");
        entry.setGroupId(0);
        entry.setGroupName("root");
        entry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE);

        entry = map(entry);

        entry.setSize(0);

        pReceiver.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(),
                entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
    }
}

From source file:org.vafer.jdeb.producers.Producers.java

/**
 * Forwards tar archive entry entry to a consumer.
 * @param consumer the consumer//from w  ww . j  av a  2s  .  c om
 * @param entry the entry to pass
 * @throws IOException
 */
static void produceDirEntry(final DataConsumer consumer, final TarArchiveEntry entry) throws IOException {
    consumer.onEachDir(entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(),
            entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
}

From source file:org.vafer.jdeb.producers.Producers.java

/**
 * Feeds input stream to data consumer using metadata from tar entry.
 * @param consumer the consumer//from  w  ww .j a  v a 2s.  co m
 * @param inputStream the stream to feed
 * @param entry the entry to use for metadata
 * @throws IOException on consume error
 */
static void produceInputStreamWithEntry(final DataConsumer consumer, final InputStream inputStream,
        final TarArchiveEntry entry) throws IOException {
    try {
        consumer.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(),
                entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:plptool.gui.ProjectDriver.java

/**
 * Open plp file specified by path./*www .j  a v a2 s  .  co  m*/
 *
 * @param path Path to project file to load.
 * @param assemble Attempt to assemble after opening (if dirty is not set)
 * @return PLP_OK on successful operation, error code otherwise
 */
public int open(String path, boolean assemble) {
    File plpFile = new File(path);
    CallbackRegistry.callback(CallbackRegistry.PROJECT_OPEN, plpFile);

    if (!plpFile.exists())
        return Msg.E("open(" + path + "): File not found.", Constants.PLP_BACKEND_PLP_OPEN_ERROR, null);

    boolean metafileFound = false;
    dirty = true;

    Msg.I("Opening " + path, null);

    if (arch != null) {
        arch.cleanup();
        arch = null;
    }
    asm = null;
    asms = new ArrayList<PLPAsmSource>();
    smods = null;
    watcher = null;
    pAttrSet = new HashMap<String, Object>();
    HashMap<String, Integer> asmFileOrder = new HashMap<String, Integer>();

    try {

        TarArchiveInputStream tIn = new TarArchiveInputStream(new FileInputStream(plpFile));
        TarArchiveEntry entry;
        byte[] image;
        String metaStr;

        // Find meta file first
        while ((entry = tIn.getNextTarEntry()) != null) {
            if (entry.getName().equals("plp.metafile")) {
                image = new byte[(int) entry.getSize()];
                tIn.read(image, 0, (int) entry.getSize());
                metaStr = new String(image);

                metafileFound = true;
                meta = metaStr;
                Scanner metaScanner;

                String lines[] = meta.split("\\r?\\n");
                if (lines[0].equals(Text.projectFileVersionString)) {

                } else {
                    Msg.W("This is not a " + Text.projectFileVersionString + " project file. Opening anyways.",
                            this);
                }

                metaScanner = new Scanner(meta);
                metaScanner.findWithinHorizon("DIRTY=", 0);
                if (metaScanner.nextInt() == 0)
                    dirty = false;
                if (metaScanner.findWithinHorizon("ARCH=", 0) != null) {
                    String temp = metaScanner.nextLine();
                    if (Config.cfgOverrideISA >= 0) { // ISA ID override, ignore the metafile
                        arch = ArchRegistry.getArchitecture(this, Config.cfgOverrideISA);
                        arch.init();
                    } else if (temp.equals("plpmips")) {
                        Msg.W("This project file was created by PLPTool version 3 or earlier. "
                                + "Meta data for this project will be updated "
                                + "with the default ISA (plpmips) when the project " + "file is saved.", this);
                        arch = ArchRegistry.getArchitecture(this, ArchRegistry.ISA_PLPMIPS);
                        arch.init();
                    } else {
                        arch = ArchRegistry.getArchitecture(this, Integer.parseInt(temp));
                        if (arch == null) {
                            Msg.W("Invalid ISA ID is specified in the project file: '" + temp
                                    + "'. Assuming PLPCPU.", this);
                            arch = ArchRegistry.getArchitecture(this, ArchRegistry.ISA_PLPMIPS);
                        }
                        arch.init();
                    }
                    arch.hook(this);
                }

                // get asm files order
                int asmOrder = 0;
                while (metaScanner.hasNext()) {
                    String asmName = metaScanner.nextLine();
                    if (asmName.endsWith(".asm")) {
                        asmFileOrder.put(asmName, new Integer(asmOrder));
                        asmOrder++;
                        asms.add(null);
                    }
                }
            }
        }

        if (!metafileFound)
            return Msg.E("No PLP metadata found.", Constants.PLP_BACKEND_INVALID_PLP_FILE, this);

        // reset the tar input stream
        tIn = new TarArchiveInputStream(new FileInputStream(plpFile));

        while ((entry = tIn.getNextTarEntry()) != null) {
            boolean handled = false;
            image = new byte[(int) entry.getSize()];
            tIn.read(image, 0, (int) entry.getSize());
            metaStr = new String(image);

            // Hook for project open for each entry
            Object[] eParams = { entry.getName(), image, plpFile };
            handled = CallbackRegistry.callback(CallbackRegistry.PROJECT_OPEN_ENTRY, eParams) || handled;

            if (entry.getName().endsWith("asm") && !entry.getName().startsWith("plp.")) {
                Integer order = (Integer) asmFileOrder.get(entry.getName());
                if (order == null)
                    Msg.W("The file '" + entry.getName() + "' is not listed in "
                            + "the meta file. This file will be removed when the project " + "is saved.", this);
                else {
                    asms.set(order, new PLPAsmSource(metaStr, entry.getName(), order));
                }

            } else if (entry.getName().equals("plp.metafile")) {
                // we've done reading the metafile

            } else if (entry.getName().equals("plp.image")) {
                binimage = new byte[(int) entry.getSize()];
                binimage = image;

            } else if (entry.getName().equals("plp.hex")) {
                hexstring = new String(image);

                // Restore bus modules states
            } else if (entry.getName().equals("plp.simconfig")) {
                Msg.D("simconfig:\n" + metaStr + "\n", 4, this);
                String lines[] = metaStr.split("\\r?\\n");
                int i;

                for (i = 0; i < lines.length; i++) {
                    String tokens[] = lines[i].split("::");

                    if (lines[i].startsWith("simRunnerDelay")) {
                        Config.simRunnerDelay = Integer.parseInt(tokens[1]);
                    }

                    if (lines[i].equals("MODS")) {
                        i++;
                        this.smods = new Preset();

                        while (i < lines.length && !lines[i].equals("END")) {
                            tokens = lines[i].split("::");
                            if (tokens.length > 4 && tokens[4].equals("noframe"))
                                smods.addModuleDefinition(Integer.parseInt(tokens[0]),
                                        Long.parseLong(tokens[2]), Long.parseLong(tokens[3]), false, false);
                            else if (tokens.length > 4)
                                smods.addModuleDefinition(Integer.parseInt(tokens[0]),
                                        Long.parseLong(tokens[2]), Long.parseLong(tokens[3]), true,
                                        Boolean.parseBoolean(tokens[5]));

                            i++;
                        }
                    }

                    if (lines[i].equals("WATCHER")) {
                        i++;
                        this.watcher = Watcher.getTableInitialModel();

                        while (i < lines.length && !lines[i].equals("END")) {
                            tokens = lines[i].split("::");
                            Object row[] = { tokens[0], tokens[1], null, null };
                            watcher.addRow(row);
                            i++;
                        }
                    }

                    if (lines[i].equals("ISASPECIFIC")) {
                        i++;

                        while (i < lines.length && !lines[i].equals("END")) {
                            tokens = lines[i].split("::");
                            arch.restoreArchSpecificSimStates(tokens);
                            i++;
                        }
                    }
                }
            } else if (handled) {

            } else {
                Msg.W("open(" + path + "): unable to process entry: " + entry.getName()
                        + ". This file will be removed when" + " you save the project.", this);
            }
        }

        tIn.close();

        if (asmFileOrder.isEmpty()) {
            return Msg.E("open(" + path + "): no .asm files found.", Constants.PLP_BACKEND_INVALID_PLP_FILE,
                    null);
        }

    } catch (Exception e) {
        Msg.trace(e);
        return Msg.E("open(" + path + "): Invalid PLP archive.", Constants.PLP_BACKEND_INVALID_PLP_FILE, null);
    }

    if (arch == null) {
        Msg.W("No ISA information specified in the archive, assuming plpmips", this);
        arch = ArchRegistry.getArchitecture(this, ArchRegistry.ISA_PLPMIPS);
        arch.init();
    }

    plpfile = new File(path);
    modified = false;
    open_asm = 0;

    for (int i = 0; i < asms.size(); i++)
        Msg.I(i + ": " + asms.get(i).getAsmFilePath(), null);

    if (g)
        refreshProjectView(false);
    if (!dirty && assemble) {
        assemble();
        asm_req = false;
    } else
        asm_req = true;

    if (g) {
        g_opts.restoreSavedOpts();
        desimulate();

        if (asm != null && asm.isAssembled())
            g_dev.enableSimControls();
        else
            g_dev.disableSimControls();

        this.setUnModified();
        updateWindowTitle();
        g_dev.updateDevelopRecentProjectList(plpFile.getAbsolutePath());
        if (g_asmview != null)
            g_asmview.dispose();
    }

    CallbackRegistry.callback(CallbackRegistry.PROJECT_OPEN_SUCCESSFUL, plpFile);
    return Constants.PLP_OK;
}

From source file:taller.mundo.BogotaMap.java

/**
 *  Rutina que permite realizar la descompresin de un contenedor tar.bz2. Durante el
 *  proceso de descompresin, la rutina informa al usuario en consola, adems 
 *  del nombre del archivo actual, el progreso de la operacin. La rutina de-
 *  tiene la ejecucin del programa si ocurre algn error.
 *  @param path La ubicacin del archivo tar.bz2 a ser descomprimido.
 **///from w ww .ja  v a 2 s.  co  m
private void uncompressTarBzip(String path) {
    String anim = "|/-\\";
    int buffersize = 1024;
    try (TarArchiveInputStream is = new TarArchiveInputStream(
            new BZip2CompressorInputStream(new FileInputStream(path)))) {
        TarArchiveEntry entry;
        while ((entry = is.getNextTarEntry()) != null) {
            int offset = 0;
            final byte[] buffer = new byte[buffersize];
            FileOutputStream fout = new FileOutputStream("./data/" + entry.getName());
            System.out.println(entry.getName());
            int size = 0;
            int n = 0;
            long total = entry.getSize();
            while (-1 != (n = is.read(buffer))) {
                double x = ((size * 1.0) / total) * 100.0;
                String progress = "\r" + anim.charAt(((int) Math.round(x)) % anim.length()) + " "
                        + Math.round(x) + "% " + String.format("(%d / %d)", size, total);
                System.out.write(progress.getBytes());
                size += n;
                fout.write(buffer, 0, n);
            }
            System.out.println();
            fout.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}