Exemplo n.º 1
0
        public long AddFileData(byte[] data, int pos, int len)
        {
            long totalSize = currentFile.CurrentSize + len;

            if (totalSize > currentFile.Size)
            {
                throw new ApplicationException("totalSize > currentFile.Size");
            }

            if (currentFile.Compress == false)
            {
                fifo.Write(data, pos, len);
            }
            else
            {
                CoreUtil.Internal.ZStream zs = currentFile.ZStream;

                byte[] srcData = Util.ExtractByteArray(data, pos, len);
                byte[] dstData = new byte[srcData.Length * 2 + 100];

                zs.next_in       = srcData;
                zs.avail_in      = srcData.Length;
                zs.next_in_index = 0;

                zs.next_out       = dstData;
                zs.avail_out      = dstData.Length;
                zs.next_out_index = 0;

                if (currentFile.Size == (currentFile.CurrentSize + len))
                {
                    zs.deflate(CoreUtil.Internal.zlibConst.Z_FINISH);
                }
                else
                {
                    zs.deflate(CoreUtil.Internal.zlibConst.Z_SYNC_FLUSH);
                }

                fifo.Write(dstData, 0, dstData.Length - zs.avail_out);

                currentFile.CompressSize += dstData.Length - zs.avail_out;

                Util.NoOP();
            }

            currentFile.CurrentSize += len;

            currentFile.Crc32 = ZipUtil.Crc32Next(data, pos, len, currentFile.Crc32);

            long ret = currentFile.Size - currentFile.CurrentSize;

            if (ret == 0)
            {
                currentFile.Crc32 = ~currentFile.Crc32;
                addFileFooter();

                currentFile = null;
            }

            return(ret);
        }