コード例 #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;
        }
コード例 #2
0
        private void paxHeaders() //throws IOException
        {
            java.io.BufferedReader         br      = new java.io.BufferedReader(new java.io.InputStreamReader(this, "UTF-8"));
            java.util.Map <String, String> headers = new java.util.HashMap <String, String>();
            // Format is "length keyword=value\n";
            while (true) // get length
            {
                int ch;
                int len  = 0;
                int read = 0;
                while ((ch = br.read()) != -1)
                {
                    read++;
                    if (ch == ' ')  // End of length string
                    // Get keyword
                    {
                        StringBuilder sb = new StringBuilder();
                        while ((ch = br.read()) != -1)
                        {
                            read++;
                            if (ch == '=')  // end of keyword
                            {
                                String keyword = sb.toString();
                                // Get rest of entry
                                char[] cbuf = new char[len - read];
                                int    got  = br.read(cbuf);
                                if (got != len - read)
                                {
                                    throw new java.io.IOException("Failed to read Paxheader. Expected " + (len - read) + " chars, read " + got);
                                }
                                String value = new String(cbuf, 0, len - read - 1); // Drop trailing NL
                                headers.put(keyword, value);
                                break;
                            }
                            sb.Append((char)ch);
                        }
                        break; // Processed single header
                    }
                    len *= 10;
                    len += ch - '0';
                }
                if (ch == -1)  // EOF
                {
                    break;
                }
            }
            getNextEntry(); // Get the actual file entry

            /*
             * The following headers are defined for Pax.
             * atime, ctime, mtime, charset: cannot use these without changing TarArchiveEntry fields
             * comment
             * gid, gname
             * linkpath
             * size
             * uid,uname
             */
            java.util.Iterator <java.util.MapNS.Entry <String, String> > hdrs = headers.entrySet().iterator();
            while (hdrs.hasNext())
            {
                java.util.MapNS.Entry <String, String> ent = hdrs.next();
                String key = ent.getKey();
                String val = ent.getValue();
                if ("path".equals(key))
                {
                    currEntry.setName(val);
                }
                else if ("linkpath".equals(key))
                {
                    currEntry.setLinkName(val);
                }
                else if ("gid".equals(key))
                {
                    currEntry.setGroupId(java.lang.Integer.parseInt(val));
                }
                else if ("gname".equals(key))
                {
                    currEntry.setGroupName(val);
                }
                else if ("uid".equals(key))
                {
                    currEntry.setUserId(java.lang.Integer.parseInt(val));
                }
                else if ("uname".equals(key))
                {
                    currEntry.setUserName(val);
                }
                else if ("size".equals(key))
                {
                    currEntry.setSize(java.lang.Long.parseLong(val));
                }
            }
        }