Ejemplo n.º 1
0
        /**
         * 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, closeArchiveEntry()
         * <B>MUST</B> be called to ensure that all buffered data
         * is completely written to the output stream.
         *
         * @param archiveEntry The TarEntry to be written to the archive.
         * @throws IOException on error
         * @throws ClassCastException if archiveEntry is not an instance of TarArchiveEntry
         */
        public override void putArchiveEntry(ArchiveEntry archiveEntry) //throws IOException
        {
            if (finished)
            {
                throw new java.io.IOException("Stream has already been finished");
            }
            TarArchiveEntry entry = (TarArchiveEntry)archiveEntry;

            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
                    TarArchiveEntry longLinkEntry = new TarArchiveEntry(TarConstants.GNU_LONGLINK,
                                                                        TarConstants.LF_GNUTYPE_LONGNAME);

                    byte[] nameBytes = ArchiveUtils.toAsciiBytes(entry.getName());
                    longLinkEntry.setSize(nameBytes.Length + 1); // +1 for NUL
                    putArchiveEntry(longLinkEntry);
                    write(nameBytes);
                    write(0); // NUL terminator
                    closeArchiveEntry();
                }
                else if (longFileMode != LONGFILE_TRUNCATE)
                {
                    throw new java.lang.RuntimeException("file name '" + entry.getName()
                                                         + "' is too long ( > "
                                                         + TarConstants.NAMELEN + " bytes)");
                }
            }

            entry.writeEntryHeader(recordBuf);
            buffer.writeRecord(recordBuf);

            currBytes = 0;

            if (entry.isDirectory())
            {
                currSize = 0;
            }
            else
            {
                currSize = entry.getSize();
            }
            currName          = entry.getName();
            haveUnclosedEntry = true;
        }
Ejemplo n.º 2
0
        /**
         * If this entry represents a file, and the file is a directory, return
         * an array of TarEntries for this entry's children.
         *
         * @return An array of TarEntry's for this entry's children.
         */
        public TarArchiveEntry[] getDirectoryEntries()
        {
            if (file == null || !file.isDirectory())
            {
                return(new TarArchiveEntry[0]);
            }

            String[]          list   = file.list();
            TarArchiveEntry[] result = new TarArchiveEntry[list.Length];

            for (int i = 0; i < list.Length; ++i)
            {
                result[i] = new TarArchiveEntry(new java.io.File(file, list[i]));
            }

            return(result);
        }
Ejemplo n.º 3
0
 protected void setCurrentEntry(TarArchiveEntry e)
 {
     currEntry = e;
 }
Ejemplo n.º 4
0
        /**
         * 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 TarArchiveEntry getNextTarEntry() //throws IOException
        {
            if (hasHitEOF)
            {
                return(null);
            }

            if (currEntry != null)
            {
                long numToSkip = entrySize - entryOffset;

                while (numToSkip > 0)
                {
                    long skipped = skip(numToSkip);
                    if (skipped <= 0)
                    {
                        throw new java.lang.RuntimeException("failed to skip current tar entry");
                    }
                    numToSkip -= skipped;
                }

                readBuf = null;
            }

            byte[] headerBuf = buffer.readRecord();

            if (headerBuf == null)
            {
                hasHitEOF = true;
            }
            else if (buffer.isEOFRecord(headerBuf))
            {
                hasHitEOF = true;
            }

            if (hasHitEOF)
            {
                currEntry = null;
            }
            else
            {
                currEntry   = new TarArchiveEntry(headerBuf);
                entryOffset = 0;
                entrySize   = currEntry.getSize();
            }

            if (currEntry != null && currEntry.isGNULongNameEntry())
            {
                // read in the name
                StringBuilder longName = new StringBuilder();
                byte[]        buf      = new byte[SMALL_BUFFER_SIZE];
                int           length   = 0;
                while ((length = read(buf)) >= 0)
                {
                    longName.Append(System.Text.ASCIIEncoding.ASCII.GetString(buf, 0, length));
                }
                getNextEntry();
                if (currEntry == null)
                {
                    // Bugzilla: 40334
                    // Malformed tar file - long entry name not followed by entry
                    return(null);
                }
                // remove trailing null terminator
                if (longName.Length > 0 &&
                    longName[longName.Length - 1] == 0)
                {
                    longName.deleteCharAt(longName.Length - 1);
                }
                currEntry.setName(longName.toString());
            }

            if (currEntry != null && currEntry.isPaxHeader())  // Process Pax headers
            {
                paxHeaders();
            }

            return(currEntry);
        }
Ejemplo n.º 5
0
 /**
  * Determine if the given entry is a descendant of this entry.
  * Descendancy is determined by the name of the descendant
  * starting with this entry's name.
  *
  * @param desc Entry to be checked as a descendent of this.
  * @return True if entry is a descendant of this.
  */
 public bool isDescendent(TarArchiveEntry desc)
 {
     return(desc.getName().StartsWith(getName()));
 }
Ejemplo n.º 6
0
 /**
  * Determine if the two entries are equal. Equality is determined
  * by the header names being equal.
  *
  * @param it Entry to be checked for equality.
  * @return True if the entries are equal.
  */
 public bool equals(TarArchiveEntry it)
 {
     return(getName().equals(it.getName()));
 }