Exemple #1
0
        /**
         * The actual data to put into local file data - without Header-ID
         * or length specifier.
         * @return get the data
         */
        public byte[] getLocalFileDataData()
        {
            // CRC will be added later
            byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
            java.lang.SystemJ.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);

            byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc
            // CheckStyle:MagicNumber OFF
            java.lang.SystemJ.arraycopy(ZipLong.getBytes(linkArray.Length),
                                        0, data, 2, WORD);

            java.lang.SystemJ.arraycopy(ZipShort.getBytes(getUserId()),
                                        0, data, 6, 2);
            java.lang.SystemJ.arraycopy(ZipShort.getBytes(getGroupId()),
                                        0, data, 8, 2);

            java.lang.SystemJ.arraycopy(linkArray, 0, data, 10, linkArray.Length);
            // CheckStyle:MagicNumber ON

            crc.reset();
            crc.update(data);
            long checksum = crc.getValue();

            byte[] result = new byte[data.Length + WORD];
            java.lang.SystemJ.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
            java.lang.SystemJ.arraycopy(data, 0, result, WORD, data.Length);
            return(result);
        }
        /**
         * Writes all necessary data for this entry.
         * @throws IOException on error
         */
        public override void closeArchiveEntry() //throws IOException
        {
            if (finished)
            {
                throw new java.io.IOException("Stream has already been finished");
            }

            if (entry == null)
            {
                throw new java.io.IOException("No current entry to close");
            }

            long realCrc = crc.getValue();

            crc.reset();

            if (entry.getMethod() == DEFLATED)
            {
                def.finish();
                while (!def.finished())
                {
                    deflate();
                }

                entry.setSize(ZipUtil.adjustToLong(def.getTotalIn()));
                entry.setCompressedSize(ZipUtil.adjustToLong(def.getTotalOut()));
                entry.setCrc(realCrc);

                def.reset();

                written += entry.getCompressedSize();
            }
            else if (raf == null)
            {
                if (entry.getCrc() != realCrc)
                {
                    throw new java.util.zip.ZipException("bad CRC checksum for entry "
                                                         + entry.getName() + ": "
                                                         + java.lang.Long.toHexString(entry.getCrc())
                                                         + " instead of "
                                                         + java.lang.Long.toHexString(realCrc));
                }

                if (entry.getSize() != written - dataStart)
                {
                    throw new java.util.zip.ZipException("bad size for entry "
                                                         + entry.getName() + ": "
                                                         + entry.getSize()
                                                         + " instead of "
                                                         + (written - dataStart));
                }
            }
            else     /* method is STORED and we used RandomAccessFile */
            {
                long size = written - dataStart;

                entry.setSize(size);
                entry.setCompressedSize(size);
                entry.setCrc(realCrc);
            }

            // If random access output, write the local file header containing
            // the correct CRC and compressed/uncompressed sizes
            if (raf != null)
            {
                long save = raf.getFilePointer();

                raf.seek(localDataStart);
                writeOut(ZipLong.getBytes(entry.getCrc()));
                writeOut(ZipLong.getBytes(entry.getCompressedSize()));
                writeOut(ZipLong.getBytes(entry.getSize()));
                raf.seek(save);
            }

            writeDataDescriptor(entry);
            entry = null;
        }
        /**
         * Closes the current ZIP archive entry and positions the underlying
         * stream to the beginning of the next entry. All per-entry variables
         * and data structures are cleared.
         * <p>
         * If the compressed size of this entry is included in the entry header,
         * then any outstanding bytes are simply skipped from the underlying
         * stream without uncompressing them. This allows an entry to be safely
         * closed even if the compression method is unsupported.
         * <p>
         * In case we don't know the compressed size of this entry or have
         * already buffered too much data from the underlying stream to support
         * uncompression, then the uncompression process is completed and the
         * end position of the stream is adjusted based on the result of that
         * process.
         *
         * @throws IOException if an error occurs
         */
        private void closeEntry() //throws IOException
        {
            if (closed)
            {
                throw new java.io.IOException("The stream is closed");
            }
            if (current == null)
            {
                return;
            }

            // Ensure all entry bytes are read
            if (bytesReadFromStream <= current.getCompressedSize() &&
                !hasDataDescriptor)
            {
                long remaining = current.getCompressedSize() - bytesReadFromStream;
                while (remaining > 0)
                {
                    long n = inJ.read(buf, 0, (int)java.lang.Math.min(buf.Length, remaining));
                    if (n < 0)
                    {
                        throw new java.io.EOFException(
                                  "Truncated ZIP entry: " + current.getName());
                    }
                    else
                    {
                        count(n);
                        remaining -= n;
                    }
                }
            }
            else
            {
                skip(java.lang.Long.MAX_VALUE);

                int inB = 0;
                if (current.getMethod() == ZipArchiveOutputStream.DEFLATED)
                {
                    inB = inf.getTotalIn();
                }
                else
                {
                    inB = readBytesOfEntry;
                }
                int diff = 0;

                // Pushback any required bytes
                if ((diff = bytesReadFromStream - inB) != 0)
                {
                    ((java.io.PushbackInputStream)inJ).unread(
                        buf, lengthOfLastRead - diff, diff);
                    pushedBackBytes(diff);
                }
            }

            if (lastStoredEntry == null && hasDataDescriptor)
            {
                readDataDescriptor();
            }

            inf.reset();
            readBytesOfEntry     = offsetInBuffer = bytesReadFromStream =
                lengthOfLastRead = 0;
            crc.reset();
            current         = null;
            lastStoredEntry = null;
        }