Search in sources :

Example 6 with TarEntry

use of org.apache.tools.tar.TarEntry in project hudson-2.x by hudson.

the class TarInputStream method getNextEntry.

/**
     * Get the next entry in this tar archive. This will skip
     * over any remaining data in the current entry, if there
     * is one, and place the input stream at the header of the
     * next entry, and read the header and instantiate a new
     * TarEntry from the header bytes and return that entry.
     * If there are no more entries in the archive, null will
     * be returned to indicate that the end of the archive has
     * been reached.
     *
     * @return The next TarEntry in the archive, or null.
     * @throws IOException on error
     */
public TarEntry getNextEntry() throws IOException {
    if (this.hasHitEOF) {
        return null;
    }
    if (this.currEntry != null) {
        long numToSkip = this.entrySize - this.entryOffset;
        if (this.debug) {
            System.err.println("TarInputStream: SKIP currENTRY '" + this.currEntry.getName() + "' SZ " + this.entrySize + " OFF " + this.entryOffset + "  skipping " + numToSkip + " bytes");
        }
        if (numToSkip > 0) {
            this.skip(numToSkip);
        }
        this.readBuf = null;
    }
    byte[] headerBuf = this.buffer.readRecord();
    if (headerBuf == null) {
        if (this.debug) {
            System.err.println("READ NULL RECORD");
        }
        this.hasHitEOF = true;
    } else if (this.buffer.isEOFRecord(headerBuf)) {
        if (this.debug) {
            System.err.println("READ EOF RECORD");
        }
        this.hasHitEOF = true;
    }
    if (this.hasHitEOF) {
        this.currEntry = null;
    } else {
        this.currEntry = new TarEntry(headerBuf);
        if (this.debug) {
            System.err.println("TarInputStream: SET CURRENTRY '" + this.currEntry.getName() + "' size = " + this.currEntry.getSize());
        }
        this.entryOffset = 0;
        this.entrySize = this.currEntry.getSize();
    }
    if (this.currEntry != null && this.currEntry.isGNULongNameEntry()) {
        // read in the name
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[256];
        int length;
        while ((length = read(buf)) >= 0) {
            baos.write(buf, 0, length);
        }
        getNextEntry();
        if (this.currEntry == null) {
            // Malformed tar file - long entry name not followed by entry
            return null;
        }
        String longName = baos.toString("UTF-8");
        // remove trailing null terminator
        if (longName.length() > 0 && longName.charAt(longName.length() - 1) == 0) {
            longName = longName.substring(0, longName.length() - 1);
        }
        this.currEntry.setName(longName);
    }
    return this.currEntry;
}
Also used : TarEntry(org.apache.tools.tar.TarEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with TarEntry

use of org.apache.tools.tar.TarEntry in project hudson-2.x by hudson.

the class TarOutputStream method putNextEntry.

/**
     * Put an entry on the output stream. This writes the entry's
     * header record and positions the output stream for writing
     * the contents of the entry. Once this method is called, the
     * stream is ready for calls to write() to write the entry's
     * contents. Once the contents are written, closeEntry()
     * <B>MUST</B> be called to ensure that all buffered data
     * is completely written to the output stream.
     *
     * @param entry The TarEntry to be written to the archive.
     * @throws IOException on error
     */
public void putNextEntry(TarEntry entry) throws IOException {
    if (entry.getName().length() >= TarConstants.NAMELEN) {
        if (longFileMode == LONGFILE_GNU) {
            // create a TarEntry for the LongLink, the contents
            // of which are the entry's name
            TarEntry longLinkEntry = new TarEntry(TarConstants.GNU_LONGLINK, TarConstants.LF_GNUTYPE_LONGNAME);
            byte[] name = entry.getName().getBytes("UTF-8");
            longLinkEntry.setSize(name.length + 1);
            putNextEntry(longLinkEntry);
            write(name);
            write(0);
            closeEntry();
        } else if (longFileMode != LONGFILE_TRUNCATE) {
            throw new RuntimeException("file name '" + entry.getName() + "' is too long ( > " + TarConstants.NAMELEN + " bytes)");
        }
    }
    entry.writeEntryHeader(this.recordBuf);
    this.buffer.writeRecord(this.recordBuf);
    this.currBytes = 0;
    if (entry.isDirectory()) {
        this.currSize = 0;
    } else {
        this.currSize = entry.getSize();
    }
    currName = entry.getName();
}
Also used : TarEntry(org.apache.tools.tar.TarEntry)

Example 8 with TarEntry

use of org.apache.tools.tar.TarEntry in project gradle by gradle.

the class TarFileTree method visitImpl.

private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TarEntry(org.apache.tools.tar.TarEntry)

Example 9 with TarEntry

use of org.apache.tools.tar.TarEntry in project gradle by gradle.

the class TarTaskOutputPacker method packMetadata.

private void packMetadata(TaskOutputOriginWriter writeMetadata, TarOutputStream outputStream) throws IOException {
    TarEntry entry = new TarEntry(METADATA_PATH);
    entry.setMode(UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writeMetadata.execute(baos);
    entry.setSize(baos.size());
    outputStream.putNextEntry(entry);
    try {
        outputStream.write(baos.toByteArray());
    } finally {
        outputStream.closeEntry();
    }
}
Also used : TarEntry(org.apache.tools.tar.TarEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with TarEntry

use of org.apache.tools.tar.TarEntry in project gradle by gradle.

the class TarTaskOutputPacker method createTarEntry.

private static void createTarEntry(String path, long lastModified, long size, int mode, TarOutputStream outputStream) throws IOException {
    TarEntry entry = new TarEntry(path);
    storeModificationTime(entry, lastModified);
    entry.setSize(size);
    entry.setMode(mode);
    outputStream.putNextEntry(entry);
}
Also used : TarEntry(org.apache.tools.tar.TarEntry)

Aggregations

TarEntry (org.apache.tools.tar.TarEntry)16 File (java.io.File)5 IOException (java.io.IOException)4 TarInputStream (org.apache.tools.tar.TarInputStream)3 IOException2 (hudson.util.IOException2)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 TarInputStream (hudson.org.apache.tools.tar.TarInputStream)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 Matcher (java.util.regex.Matcher)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)1 TextField (org.apache.lucene.document.TextField)1 TarOutputStream (org.apache.tools.tar.TarOutputStream)1