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

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

Introduction

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

Prototype

public TarArchiveEntry(File file, String fileName) 

Source Link

Document

Construct an entry for a file.

Usage

From source file:com.shopzilla.hadoop.repl.commands.util.ClusterStateManager.java

private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir)
        throws IOException {
    // Create an entry for the file
    taos.putArchiveEntry(new TarArchiveEntry(file, dir + File.separator + file.getName()));
    if (file.isFile()) {
        // Add the file to the archive
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);/*w  w w . j a  v  a 2s .  c  o m*/
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        // close the archive entry
        taos.closeArchiveEntry();
        // go through all the files in the directory and using recursion, add them to the archive
        for (File childFile : file.listFiles()) {
            addFilesToCompression(taos, childFile, dir + File.separator + file.getName());
        }
    }
}

From source file:eu.eubrazilcc.lvl.core.io.FileCompressor.java

private static void addFile(final TarArchiveOutputStream tarOutputStream, final String path, final String base)
        throws IOException {
    final File file = new File(path);
    final String entryName = base + file.getName();
    final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);

    tarOutputStream.setLongFileMode(LONGFILE_GNU);
    tarOutputStream.putArchiveEntry(tarEntry);

    if (file.isFile()) {
        copy(new FileInputStream(file), tarOutputStream);
        tarOutputStream.closeArchiveEntry();
    } else {/*  ww  w. j a  va2  s  .  com*/
        tarOutputStream.closeArchiveEntry();
        final File[] children = file.listFiles();
        if (children != null) {
            for (final File child : children) {
                addFile(tarOutputStream, child.getAbsolutePath(), entryName + separator);
            }
        }
    }
}

From source file:divconq.ctp.stream.UntarStream.java

@Override
public ReturnOption handle(FileDescriptor file, ByteBuf data) {
    if (file == FileDescriptor.FINAL)
        return this.downstream.handle(file, data);

    ByteBuf in = data;// w ww . j  a v  a2s. c om

    if (in != null) {
        while (in.isReadable()) {
            switch (this.tstate) {
            case RECORD:
                // starting a new record
                if (in.readableBytes() < TarConstants.DEFAULT_RCDSIZE - this.partialLength) {
                    int offset = this.partialLength;

                    this.partialLength += in.readableBytes();

                    in.readBytes(this.header_buffer, offset, in.readableBytes());

                    continue;
                }

                in.readBytes(this.header_buffer, this.partialLength,
                        TarConstants.DEFAULT_RCDSIZE - this.partialLength);

                this.partialLength = 0;

                //in.readBytes(this.header_buffer, 0, this.header_buffer.length);

                boolean hasHitEOF = this.isEOFRecord(this.header_buffer);

                // if we hit this twice in a row we are at the end - however, source will send FINAL anyway so we don't really care
                if (hasHitEOF) {
                    this.currEntry = null;
                    continue;
                }

                try {
                    this.currEntry = new TarArchiveEntry(this.header_buffer, this.encoding);
                } catch (Exception x) {
                    OperationContext.get().getTaskRun().kill("Error detected parsing the header: " + x);
                    in.release();
                    return ReturnOption.DONE;
                }

                this.tstate = UntarState.XTRAS;
            case XTRAS:
                if (!in.isReadable())
                    continue;

                // TODO support long names and such - see org.apache.commons.compress.archivers.tar.TarArchiveInputStream
                if (this.currEntry.isGNULongLinkEntry()) {
                    /* 
                      byte[] longLinkData = getLongNameData();
                      if (longLinkData == null) {
                          // Bugzilla: 40334
                          // Malformed tar file - long link entry name not followed by
                          // entry
                          return null;
                      }
                      currEntry.setLinkName(encoding.decode(longLinkData));
                      */

                    OperationContext.get().getTaskRun().kill("long link currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isGNULongNameEntry()) {
                    /* 
                      byte[] longNameData = getLongNameData();
                      if (longNameData == null) {
                          // Bugzilla: 40334
                          // Malformed tar file - long entry name not followed by
                          // entry
                          return null;
                      }
                      currEntry.setName(encoding.decode(longNameData));
                      */

                    OperationContext.get().getTaskRun().kill("long name currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isPaxHeader()) {
                    // Process Pax headers
                    /* 
                      paxHeaders();
                      */

                    OperationContext.get().getTaskRun().kill("pax currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isGNUSparse()) {
                    // Process sparse files
                    /* 
                      readGNUSparse();
                      */

                    OperationContext.get().getTaskRun().kill("sparse currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                this.tstate = UntarState.PREP;
            case PREP:
                if (!in.isReadable())
                    continue;

                // TODO remove
                System.out.println("name: " + this.currEntry.getName());
                System.out.println("size: " + this.currEntry.getSize());
                System.out.println("modified: " + this.currEntry.getModTime());

                // If the size of the next element in the archive has changed
                // due to a new size being reported in the posix header
                // information, we update entrySize here so that it contains
                // the correct value.
                long entrySize = this.currEntry.getSize();
                this.remainContent = entrySize;

                long numRecords = (entrySize / this.header_buffer.length) + 1;
                this.remainSkip = (numRecords * this.header_buffer.length) - entrySize;

                // grab as much as we can from the current buffer
                int readSize = (int) Math.min(this.remainContent, in.readableBytes());
                this.remainContent -= readSize;

                // handle empty files too
                if ((readSize > 0) || (this.remainContent == 0)) {
                    System.out.println("reading content: " + readSize);

                    ByteBuf out = in.copy(in.readerIndex(), readSize);

                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize);
                    this.remainSkip -= skipSize;

                    in.skipBytes(readSize + skipSize);

                    this.nextMessage(out);
                }

                this.tstate = UntarState.CONTENT;
            case CONTENT:
                if (!in.isReadable())
                    continue;

                // check if there is still content left in the entry we were last reading from
                if (this.remainContent > 0) {
                    readSize = (int) Math.min(this.remainContent, in.readableBytes());
                    this.remainContent -= readSize;

                    //System.out.println("reading content: " + readSize);

                    //ByteBuf out = Hub.instance.getBufferAllocator().heapBuffer((int) readSize);

                    ByteBuf out = in.copy(in.readerIndex(), readSize);

                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize);
                    this.remainSkip -= skipSize;

                    //System.out.println("skipping content: " + skipSize);

                    in.skipBytes(readSize + skipSize);

                    this.nextMessage(out);
                }

                if (this.remainContent > 0)
                    continue;

                this.currEntry = null;

                this.tstate = UntarState.SKIP;
            case SKIP:
                if (!in.isReadable())
                    continue;

                // check if there is still padding left in the entry we were last reading from
                if (this.remainSkip > 0) {
                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes());
                    this.remainSkip -= skipSize;

                    //System.out.println("skipping content: " + skipSize);

                    in.skipBytes((int) skipSize);
                }

                if (this.remainSkip > 0)
                    continue;

                this.tstate = UntarState.RECORD;
            }
        }

        in.release();
    }

    // write all messages in the queue
    while (this.outlist.size() > 0) {
        ReturnOption ret = this.downstream.handle(this.outlist.remove(0), this.outbuf.remove(0));

        if (ret != ReturnOption.CONTINUE)
            return ret;
    }

    return ReturnOption.CONTINUE;
}

From source file:com.netflix.spinnaker.halyard.core.registry.v1.LocalDiskProfileReader.java

public InputStream readArchiveProfileFrom(Path profilePath) throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);

    ArrayList<Path> filePathsToAdd = java.nio.file.Files
            .walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
            .filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));

    for (Path path : filePathsToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
        tarArchive.putArchiveEntry(tarEntry);
        IOUtils.copy(Files.newInputStream(path), tarArchive);
        tarArchive.closeArchiveEntry();/*from www.ja v  a2 s  . c  o m*/
    }

    tarArchive.finish();
    tarArchive.close();

    return new ByteArrayInputStream(os.toByteArray());
}

From source file:divconq.ctp.stream.TarStream.java

@Override
public ReturnOption handle(FileDescriptor file, ByteBuf data) {
    if (file == FileDescriptor.FINAL) {
        if (this.tstream == null)
            return this.downstream.handle(file, data);

        this.finalflag = true;
    }/*from   w  w  w.  j a v a 2s  .c  o m*/

    // I don't think tar cares about folder entries at this stage - tar is for file content only
    // folder scanning is upstream in the FileSourceStream and partners
    // TODO try with ending / to file name
    if (file.isFolder())
        return ReturnOption.CONTINUE;

    // init if not set for this round of processing 
    if (this.tstream == null) {
        this.bstream = new CyclingByteBufferOutputStream();
        this.tstream = new TarArchiveOutputStream(this.bstream);
        this.tstream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    }

    ByteBuf in = data;
    ByteBuf out = null;

    // always allow for a header (512) and/or footer (1024) in addition to content
    int sizeEstimate = (in != null) ? in.readableBytes() + 2048 : 2048;
    out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate);

    this.bstream.installBuffer(out);

    // TODO if there is no output available to send and not EOF then just request more,
    // no need to send a message that is empty and not EOF

    FileDescriptor blk = new FileDescriptor();

    if (StringUtil.isNotEmpty(this.lastpath)) {
        blk.setPath(this.lastpath);
    } else {
        if (file.hasPath())
            this.lastpath = "/"
                    + (StringUtil.isNotEmpty(this.nameHint) ? this.nameHint : file.path().getFileName())
                    + ".tar";
        else if (StringUtil.isNotEmpty(this.nameHint))
            this.lastpath = "/" + this.nameHint + ".tar";
        else
            this.lastpath = "/" + FileUtil.randomFilename() + ".tar";

        blk.setPath(this.lastpath);
    }

    blk.setModTime(System.currentTimeMillis());

    if (!this.archiveopenflag && !this.finalflag) {
        TarArchiveEntry tentry = new TarArchiveEntry(file.getPath().toString().substring(1), true);
        tentry.setSize(file.getSize());
        tentry.setModTime(file.getModTime());

        try {
            this.tstream.putArchiveEntry(tentry);
        } catch (IOException x) {
            if (in != null)
                in.release();

            out.release();
            OperationContext.get().getTaskRun().kill("Problem writing tar entry: " + x);
            return ReturnOption.DONE;
        }

        this.archiveopenflag = true;
    }

    if (in != null)
        try {
            this.tstream.write(in.array(), in.arrayOffset(), in.writerIndex());
        } catch (IOException x) {
            in.release();
            out.release();
            OperationContext.get().getTaskRun().kill("Problem writing tar body: " + x);
            return ReturnOption.DONE;
        }

    if (file.isEof()) {
        try {
            this.tstream.closeArchiveEntry();
        } catch (IOException x) {
            if (in != null)
                in.release();

            out.release();
            OperationContext.get().getTaskRun().kill("Problem closing tar entry: " + x);
            return ReturnOption.DONE;
        }

        this.archiveopenflag = false;
    }

    if (in != null)
        in.release();

    if (file == FileDescriptor.FINAL) {
        blk.setEof(true);

        try {
            this.tstream.close();
        } catch (IOException x) {
            //in.release();
            out.release();
            OperationContext.get().getTaskRun().kill("Problem closing tar stream: " + x);
            return ReturnOption.DONE;
        }

        this.tstream = null;
        this.bstream = null;
    } else
        this.bstream.uninstallBuffer(); // we are done with out forever, don't reference it

    System.out.println("tar sending: " + out.readableBytes());

    ReturnOption v = this.downstream.handle(blk, out);

    if (!this.finalflag)
        return v;

    if (v == ReturnOption.CONTINUE)
        return this.downstream.handle(FileDescriptor.FINAL, null);

    return ReturnOption.DONE;
}

From source file:com.codenvy.commons.lang.TarUtils.java

private static void addDirectoryEntry(TarArchiveOutputStream tarOut, String entryName, File directory,
        long modTime) throws IOException {
    final TarArchiveEntry tarEntry = new TarArchiveEntry(directory, entryName);
    if (modTime >= 0) {
        tarEntry.setModTime(modTime);/*from   w  w  w.ja v a  2 s.  c  om*/
    }
    tarOut.putArchiveEntry(tarEntry);
    tarOut.closeArchiveEntry();
}

From source file:algorithm.TarPackaging.java

private void archiveFile(File carrier, TarArchiveOutputStream tarOutputStream)
        throws IOException, FileNotFoundException {
    TarArchiveEntry tarEntry = new TarArchiveEntry(carrier, carrier.getName());
    tarOutputStream.putArchiveEntry(tarEntry);
    FileInputStream inputStream = new FileInputStream(carrier);
    IOUtils.copy(inputStream, tarOutputStream);
    tarOutputStream.closeArchiveEntry();
    inputStream.close();/*from www  .ja v a2 s .  co m*/
}

From source file:com.codenvy.commons.lang.TarUtils.java

private static void addFileEntry(TarArchiveOutputStream tarOut, String entryName, File file, long modTime)
        throws IOException {
    final TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
    if (modTime >= 0) {
        tarEntry.setModTime(modTime);//from   w  w w  .  j a  va2  s .c  o  m
    }
    tarOut.putArchiveEntry(tarEntry);
    try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
        final byte[] buf = new byte[BUF_SIZE];
        int r;
        while ((r = in.read(buf)) != -1) {
            tarOut.write(buf, 0, r);
        }
    }
    tarOut.closeArchiveEntry();
}

From source file:com.flurry.proguard.UploadProGuardMapping.java

/**
 * Create a gzipped tar archive containing the ProGuard mapping file
 *
 * @param file the mapping.txt file// w  ww .j  a v  a 2  s  . c  o  m
 * @param uuid the build uuid
 * @return the tar-gzipped archive
 */
private static File createArchive(File file, String uuid) {
    try {
        File tarZippedFile = File.createTempFile("tar-zipped-file", ".tgz");
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(tarZippedFile))));
        taos.putArchiveEntry(new TarArchiveEntry(file, uuid + ".txt"));
        IOUtils.copy(new FileInputStream(file), taos);
        taos.closeArchiveEntry();
        taos.finish();
        taos.close();
        return tarZippedFile;
    } catch (IOException e) {
        failWithError("IO Exception while trying to tar and zip the file.", e);
        return null;
    }
}

From source file:com.linuxbox.enkive.workspace.searchFolder.SearchFolder.java

/**
 * Writes a tar.gz file to the provided outputstream
 * /*  w ww .  j av  a 2  s.  c o  m*/
 * @param outputStream
 * @throws IOException
 */
public void exportSearchFolder(OutputStream outputStream) throws IOException {
    BufferedOutputStream out = new BufferedOutputStream(outputStream);
    GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out);
    TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);

    File mboxFile = File.createTempFile("mbox-export", ".mbox");
    BufferedWriter mboxWriter = new BufferedWriter(new FileWriter(mboxFile));
    // Write mbox to tempfile?
    for (String messageId : getMessageIds()) {
        try {
            Message message = retrieverService.retrieve(messageId);

            mboxWriter.write("From " + message.getDateStr() + "\r\n");
            BufferedReader reader = new BufferedReader(new StringReader(message.getReconstitutedEmail()));
            String tmpLine;
            while ((tmpLine = reader.readLine()) != null) {
                if (tmpLine.startsWith("From "))
                    mboxWriter.write(">" + tmpLine);
                else
                    mboxWriter.write(tmpLine);
                mboxWriter.write("\r\n");
            }
        } catch (CannotRetrieveException e) {
            // Add errors to report
            // if (LOGGER.isErrorEnabled())
            // LOGGER.error("Could not retrieve message with id"
            // + messageId);
        }
    }
    mboxWriter.flush();
    mboxWriter.close();
    // Add mbox to tarfile
    TarArchiveEntry mboxEntry = new TarArchiveEntry(mboxFile, "filename.mbox");
    tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tOut.putArchiveEntry(mboxEntry);
    IOUtils.copy(new FileInputStream(mboxFile), tOut);
    tOut.flush();
    tOut.closeArchiveEntry();
    mboxWriter.close();
    mboxFile.delete();
    // Create report in tempfile?

    // Add report to tarfile

    // Close out stream
    tOut.finish();
    outputStream.flush();
    tOut.close();
    outputStream.close();

}