コード例 #1
0
        /// <summary>
        /// Closes the current ZIP entry and positions the stream for writing
        /// the next entry. </summary>
        /// <exception cref="ZipException"> if a ZIP format error has occurred </exception>
        /// <exception cref="IOException"> if an I/O error has occurred </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void closeEntry() throws java.io.IOException
        public virtual void CloseEntry()
        {
            EnsureOpen();
            if (Current != null)
            {
                ZipEntry e = Current.Entry;
                switch (e.Method_Renamed)
                {
                case DEFLATED:
                    Def.Finish();
                    while (!Def.Finished())
                    {
                        Deflate();
                    }
                    if ((e.Flag & 8) == 0)
                    {
                        // verify size, compressed size, and crc-32 settings
                        if (e.Size_Renamed != Def.BytesRead)
                        {
                            throw new ZipException("invalid entry size (expected " + e.Size_Renamed + " but got " + Def.BytesRead + " bytes)");
                        }
                        if (e.Csize != Def.BytesWritten)
                        {
                            throw new ZipException("invalid entry compressed size (expected " + e.Csize + " but got " + Def.BytesWritten + " bytes)");
                        }
                        if (e.Crc_Renamed != Crc.Value)
                        {
                            throw new ZipException("invalid entry CRC-32 (expected 0x" + e.Crc_Renamed.ToString("x") + " but got 0x" + Crc.Value.ToString("x") + ")");
                        }
                    }
                    else
                    {
                        e.Size_Renamed = Def.BytesRead;
                        e.Csize        = Def.BytesWritten;
                        e.Crc_Renamed  = Crc.Value;
                        WriteEXT(e);
                    }
                    Def.Reset();
                    Written += e.Csize;
                    break;

                case STORED:
                    // we already know that both e.size and e.csize are the same
                    if (e.Size_Renamed != Written - Locoff)
                    {
                        throw new ZipException("invalid entry size (expected " + e.Size_Renamed + " but got " + (Written - Locoff) + " bytes)");
                    }
                    if (e.Crc_Renamed != Crc.Value)
                    {
                        throw new ZipException("invalid entry crc-32 (expected 0x" + e.Crc_Renamed.ToString("x") + " but got 0x" + Crc.Value.ToString("x") + ")");
                    }
                    break;

                default:
                    throw new ZipException("invalid compression method");
                }
                Crc.Reset();
                Current = null;
            }
        }
コード例 #2
0
        /// <summary>
        /// Finishes writing compressed data to the output stream without closing
        /// the underlying stream. Use this method when applying multiple filters
        /// in succession to the same output stream. </summary>
        /// <exception cref="IOException"> if an I/O error has occurred </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void finish() throws java.io.IOException
        public override void Finish()
        {
            if (!Def.Finished())
            {
                Def.Finish();
                while (!Def.Finished())
                {
                    int len = Def.Deflate(Buf, 0, Buf.Length);
                    if (Def.Finished() && len <= Buf.Length - TRAILER_SIZE)
                    {
                        // last deflater buffer. Fit trailer at the end
                        WriteTrailer(Buf, len);
                        len = len + TRAILER_SIZE;
                        @out.Write(Buf, 0, len);
                        return;
                    }
                    if (len > 0)
                    {
                        @out.Write(Buf, 0, len);
                    }
                }
                // if we can't fit the trailer at the end of the last
                // deflater buffer, we write it separately
                sbyte[] trailer = new sbyte[TRAILER_SIZE];
                WriteTrailer(trailer, 0);
                @out.Write(trailer);
            }
        }