Ejemplo n.º 1
0
        /// <summary>
        /// Compress the input data to an output stream. The first four bytes
        /// is a UInt32 (in LSB format) that contains the size of the *decompressed*
        /// data.
        /// </summary>
        /// <param name="inputData">An array of bytes to compress</param>
        /// <param name="nNumInputBytes">The number of input bytes from inputData to compress</param>
        /// <param name="outStream">The stream to write the compressed bytes to.</param>
        /// <returns>The length of the compressed data (not including the length value
        /// that is written to the beginning of the stream)</returns>
        public static int Compress(byte[] inputData, int nNumInputBytes, Stream outStream)
        {
            if (inputData.Length == 0 || nNumInputBytes == 0)
            {
                throw new InvalidDataException("ZLib.Compress: No input data to compress.");
            }

            long nMaxCompressedDataLen = nNumInputBytes + ((nNumInputBytes / 1000) + 1) + 12 + sizeof(int);

            byte[] outBuff = new byte[nMaxCompressedDataLen];
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater compresser =
                new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
            compresser.SetInput(inputData);
            compresser.Finish();

            int nCompressedDataLength = compresser.Deflate(outBuff);

            BinaryWriter writer = new BinaryWriter(outStream);

            writer.Write(nNumInputBytes);
            writer.Write(outBuff, 0, nCompressedDataLength);

            writer.Flush();

            return(nCompressedDataLength);
        }
Ejemplo n.º 2
0
        public void WriteFile(Stream outstream, FileInArchive file, byte[] data)
        {
            MemoryStream inputMS  = new MemoryStream(data);
            MemoryStream outputMS = new MemoryStream();

            byte[] output_buffer = new byte[0];

            if (file.Descriptor.CompressionMethod == 1) //ZLib compression
            {
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
                ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream defstream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(outputMS, def);
                defstream.Write(data, 0, data.Length);
                defstream.Flush();
                defstream.Finish();
                output_buffer = outputMS.GetBuffer();
                file.Descriptor.UncompressedSize = (uint)inputMS.Length;

                Write(outstream, file, outputMS);
            }
            else if (file.Descriptor.CompressionMethod == 0)  //No compression
            {
                file.Descriptor.CompressionMethod = 0;
                inputMS.CopyTo(outputMS);
                file.Descriptor.UncompressedSize = (uint)inputMS.Length;

                Write(outstream, file, outputMS);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 字节数组压缩
        /// 返回:已压缩的字节数组
        /// </summary>
        /// <param name="data">待压缩的字节数组</param>
        /// <returns></returns>
        public static byte[] CompressBytes(byte[] data)
        {
            var f = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(ICSharpCode.SharpZipLib.Zip.Compression
                                                                         .Deflater.BEST_COMPRESSION);

            f.SetInput(data);
            f.Finish();

            var o = new MemoryStream(data.Length);

            try
            {
                var buf = new byte[1024];
                while (!f.IsFinished)
                {
                    var got = f.Deflate(buf);
                    o.Write(buf, 0, got);
                }
            }
            finally
            {
                o.Close();
            }

            return(o.ToArray());
        }
Ejemplo n.º 4
0
        public LegacyBBeBObject CreateLegacyTextObject()
        {
            m_PageBuffer.putTag(TagId.EndPage);

            byte[]      output = m_PageBuffer.GetBuffer();
            ObjectFlags flags  = 0;
            int         len    = (int)m_PageBuffer.Position;

            if (len > k_MinUncompressedLen)
            {
                // Allocate a buffer to compress into
                output = new byte[len];

                // Stash uncompressed size
                ByteBuffer.PackInt(output, 0, len);

                // Deflate text
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater compresser =
                    new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
                compresser.SetInput(m_PageBuffer.GetBuffer(), 0, len);
                compresser.Finish();
                len   = compresser.Deflate(output, 4, output.Length - 4) + 4;
                flags = ObjectFlags.COMPRESSED;
            }

            return(new LegacyBBeBObject(m_Book, ObjectType.Text, flags, output, len));
        }
Ejemplo n.º 5
0
        public static byte[] LowLevelCompress(byte[] bytes, Shell.EndianFormat byteOrder)
        {
            Contract.Requires <ArgumentNullException>(bytes != null);
            Contract.Ensures(Contract.Result <byte[]>() != null);

            byte[] result = new byte[sizeof(int)];
            // Setup the decompressed size header
            byte[] size_bytes = BitConverter.GetBytes(bytes.Length);
            if (!byteOrder.IsSameAsRuntime())
            {
                Bitwise.ByteSwap.SwapInt32(size_bytes, 0);
            }
            Array.Copy(size_bytes, result, size_bytes.Length);

            var zip = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, false);

            {
                zip.SetInput(bytes);
                zip.Finish();
                byte[] temp            = new byte[bytes.Length];
                int    compressed_size = zip.Deflate(temp);

                Contract.Assert(compressed_size <= bytes.Length);
                Array.Resize(ref result, sizeof(int) + compressed_size);
                Array.Copy(temp, 0, result, sizeof(int), compressed_size);
            }
            return(result);
        }
Ejemplo n.º 6
0
        public static int DoDeflate(ref byte[] pmSource, int pmOffSet, int pmLength, int pmCompressionLevel)
        {
            int deflatedLength = 0;

            ICSharpCode.SharpZipLib.Zip.Compression.Deflater checkDeflater = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(pmCompressionLevel);
            deflatedLength = checkDeflater.Deflate(pmSource, pmOffSet, pmLength);

            return(deflatedLength);
        }
Ejemplo n.º 7
0
Archivo: Utils.cs Proyecto: biuken/eAnt
        private void CompresNowZip()
        {
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater def =
                new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
                    ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, true);
            Stream aux = new MemoryStream();

            byte[] data = new byte[this.m_streaminput.Length];           //minimo el tamaño sin comprimir.
            int    size = 0;

            try
            {
                def.SetInput(this.m_byteinput);
                def.Finish();
                size = def.Deflate(data);
                if (size == 0)
                {
                    while (def.IsFinished == false)
                    {
                        if (def.IsNeedingInput)
                        {
                            Exception e = new Exception("Tamaño muy pequeño para comprimir");
                            break;
                        }
                        else
                        {
                            size = def.Deflate(data);
                            System.Threading.Thread.Sleep(2000);
                        }
                    }
                }
                def.Flush();
            }
            catch (ZipException e)
            {
                this.m_byteoutput   = null;
                this.m_streamoutput = null;
                Debug.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                this.m_byteoutput   = null;
                this.m_streamoutput = null;
                Debug.WriteLine(e.Message);
            }
            finally
            {
                this.m_byteoutput   = null;
                this.m_byteoutput   = new byte[size];
                this.m_streamoutput = new MemoryStream(size);
                this.m_streamoutput.Write(data, 0, size);
                this.m_streamoutput.Seek(0, SeekOrigin.Begin);
                this.m_streamoutput.Read(this.m_byteoutput, 0, size);
                this.m_streamoutput.Seek(0, SeekOrigin.Begin);
            }
        }
Ejemplo n.º 8
0
 public static byte[] Compress(byte[] byteData)
 {
     byte[] compressedData = null;
     if (byteData != null)
     {
         using (MemoryStream ms = new MemoryStream())
         {
             ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl =
                 new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9, false);
             using (Stream s =
                        new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms, defl))
                 s.Write(byteData, 0, byteData.Length);
             compressedData = ms.ToArray();
         }
     }
     return(compressedData);
 }
Ejemplo n.º 9
0
 public static byte[] Compress(byte[] byteData)
 {
   byte[] compressedData = null;
   if (byteData != null)
   {
     using (MemoryStream ms = new MemoryStream())
     {
       ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl =
        new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9, false);
       using (Stream s =
           new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms, defl))
         s.Write(byteData, 0, byteData.Length);
       compressedData = ms.ToArray();
     }
   }
   return compressedData;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// deflater压缩
 /// </summary>
 /// <param name="inputByte"></param>
 /// <returns></returns>
 public static byte[] deflater(byte[] inputByte)
 {
     byte[] temp = new byte[1024];
     MemoryStream memory = new MemoryStream();
     ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
     def.SetInput(inputByte);
     def.Finish();
     while (!def.IsFinished)
     {
         int extracted = def.Deflate(temp);
         if (extracted > 0)
         {
             memory.Write(temp, 0, extracted);
         }
         else
         {
             break;
         }
     }
     return memory.ToArray();
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Replaces a file by a file
        /// </summary>
        /// <param name="archFile">file to replace</param>
        /// <param name="newFile">new file</param>
        public void ReplaceFile(FileInArchive archFile, FileStream newFile)
        {
            byte[] newFileBuffer = new byte[newFile.Length];
            newFile.Read(newFileBuffer, 0, newFileBuffer.Length);

            MemoryStream inputMS  = new MemoryStream(newFileBuffer);
            MemoryStream outputMS = new MemoryStream();

            byte[] output_buffer = new byte[0];

            if (archFile.descriptor.compressionMethod == 1 && false) //ZLib compression
            {
                try
                {
                    ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
                    ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream defstream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(outputMS, def);
                    defstream.Write(newFileBuffer, 0, newFileBuffer.Length);
                    defstream.Flush();
                    defstream.Finish();

                    output_buffer = outputMS.GetBuffer();
                }
                catch
                {
                }

                archFile.descriptor.uncompressedSize = (uint)inputMS.Length;

                WriteFileToArchive(archFile, outputMS);
            }
            else if (archFile.descriptor.compressionMethod == 0 || true) //No compression
            {
                archFile.descriptor.compressionMethod = 0;
                CopyStream(inputMS, outputMS);

                archFile.descriptor.uncompressedSize = (uint)inputMS.Length;

                WriteFileToArchive(archFile, outputMS);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// deflater压缩
        /// </summary>
        /// <param name="inputByte"></param>
        /// <returns></returns>
        public static byte[] deflater(byte[] inputByte)
        {
            byte[]       temp   = new byte[1024];
            MemoryStream memory = new MemoryStream();

            ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
            def.SetInput(inputByte);
            def.Finish();
            while (!def.IsFinished)
            {
                int extracted = def.Deflate(temp);
                if (extracted > 0)
                {
                    memory.Write(temp, 0, extracted);
                }
                else
                {
                    break;
                }
            }
            return(memory.ToArray());
        }
Ejemplo n.º 13
0
        public static string ToDeflatedCompressedBase64String(string inp, string ns)
        {
            StringBuilder sb         = new StringBuilder();
            TextWriter    textWriter = (TextWriter) new StringWriter(sb);

            new XmlSerializer(typeof(string), ns).Serialize(textWriter, (object)inp);
            textWriter.Close();
            sb.ToString();
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater deflater = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(1, true);
            MemoryStream memoryStream = new MemoryStream();

            byte[] numArray = new byte[256];
            deflater.SetInput(new UnicodeEncoding().GetBytes(inp));
            deflater.Flush();
            int count;

            do
            {
                count = deflater.Deflate(numArray, 0, numArray.Length);
                memoryStream.Write(numArray, 0, count);
            }while (count > 0);
            return(Convert.ToBase64String(memoryStream.ToArray()));
        }
Ejemplo n.º 14
0
        public static byte[] LowLevelCompress(byte[] bytes, int level,
                                              out uint adler, byte[] compressedBytes,
                                              bool trimCompressedBytes = true, bool noZlibHeaderOrFooter = true)
        {
            int compressed_size;

            var zip = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(level, noZlibHeaderOrFooter);

            {
                zip.SetInput(bytes);
                zip.Finish();
                compressed_size = zip.Deflate(compressedBytes);
                adler           = (uint)zip.Adler;

                if (trimCompressedBytes)
                {
                    byte[] cmp_data = compressedBytes;
                    Array.Resize(ref cmp_data, compressed_size);
                    compressedBytes = cmp_data;
                }
            }

            return(compressedBytes);
        }
Ejemplo n.º 15
0
 private void CompresNowZip()
 {
     ICSharpCode.SharpZipLib.Zip.Compression.Deflater def =
             new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
             ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, true);
     Stream aux = new MemoryStream();
     byte[] data = new byte[this.m_streaminput.Length]; //minimo el tamaño sin comprimir.
     int size = 0;
     try
     {
         def.SetInput(this.m_byteinput);
         def.Finish();
         size = def.Deflate(data);
         if (size == 0)
             while (def.IsFinished == false)
             {
                 if (def.IsNeedingInput)
                 {
                     Exception e = new Exception("Tamaño muy pequeño para comprimir");
                     break;
                 }
                 else
                 {
                     size = def.Deflate(data);
                     System.Threading.Thread.Sleep(2000);
                 }
             }
         def.Flush();
     }
     catch (ZipException e)
     {
         this.m_byteoutput = null;
         this.m_streamoutput = null;
         Debug.WriteLine(e.Message);
     }
     catch (Exception e)
     {
         this.m_byteoutput = null;
         this.m_streamoutput = null;
         Debug.WriteLine(e.Message);
     }
     finally
     {
         this.m_byteoutput = null;
         this.m_byteoutput = new byte[size];
         this.m_streamoutput = new MemoryStream(size);
         this.m_streamoutput.Write(data, 0, size);
         this.m_streamoutput.Seek(0, SeekOrigin.Begin);
         this.m_streamoutput.Read(this.m_byteoutput, 0, size);
         this.m_streamoutput.Seek(0, SeekOrigin.Begin);
     }
 }
Ejemplo n.º 16
0
        void PatchPck(string pckFilename, string filename, byte[] data)
        {
            FileStream fs = new FileStream(FwInstallPath+pckFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            BinaryWriter bw = new BinaryWriter(fs);
            BinaryReader br = new BinaryReader(fs);

            fs.Seek(-8,SeekOrigin.End);
            int entryCount = br.ReadInt32();

            fs.Seek(-272,SeekOrigin.End);
            int fileTableOffset = br.ReadInt32() ^ KEY_1;
            fs.Seek(fileTableOffset,SeekOrigin.Begin);

            int entrySize;
            fileTableEntry[] fileTable = new fileTableEntry[entryCount];
            bool[] toEdit = new bool[entryCount];
            int t = 0;
            int editedFiles = 0;
            for (int i = 0; i < entryCount; i++) {
                entrySize = br.ReadInt32() ^ KEY_1;
                entrySize = br.ReadInt32() ^ KEY_2;

                byte[] hdata = br.ReadBytes(entrySize);
               //
                if(entrySize < 276)
                {
                    fileTable[i] = readTableEntry(hdata, entrySize, true);
                    t++;
                }
                // no zlib
                else
                {
                    fileTable[i] = readTableEntry(hdata, entrySize, false);
                }

            }

            textBox1.Text += entryCount.ToString()+"\r\n";

            fs.Seek(fileTableOffset,SeekOrigin.Begin); // useless ??

            int lastEntryPosition = fileTableOffset;

            //int filesCount = (int)files.Length;

            for (int i = 0; i < entryCount; i++) {
                if ( fileTable[i].filePath != filename ) continue;
                nb++;
                int newFileDataDecompressedSize = (int)data.Length;

                MemoryStream ms = new MemoryStream();
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(1,false);
                Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms,defl);
                s.Write(data, 0, data.Length);
                s.Close();
                byte[] dataComp = (byte[])ms.ToArray();
                ms.Close();
                int newFileDataCompressedSize = (int)dataComp.Length;

                // test de remplacer un gros png par un truc tout petit incompresible

                if ( newFileDataCompressedSize <= fileTable[i].fileDataCompressedSize ){

                    fs.Seek(fileTable[i].fileDataOffset,SeekOrigin.Begin);
                    bw.Write(dataComp);
                    textBox1.Text += fileTable[i].filePath+" Replaced\r\n";
                }
                else {
                    fileTable[i].fileDataOffset = lastEntryPosition;
                    fs.Seek(fileTable[i].fileDataOffset,SeekOrigin.Begin);
                    bw.Write(dataComp);
                    lastEntryPosition = (int)fs.Position;
                    textBox1.Text += fileTable[i].filePath+" Moved to end\r\n";
                }
                fileTable[i].fileDataCompressedSize = newFileDataCompressedSize;
                fileTable[i].fileDataDecompressedSize = newFileDataDecompressedSize;

                //ShowProgress(entryCount, i+1);

            }

            fs.Seek(lastEntryPosition,SeekOrigin.Begin);

            fileTableOffset = (int)fs.Position;

            for (int i = 0; i < entryCount; i++) {

                data = new byte[276];
                byte[] name = System.Text.Encoding.GetEncoding(936).GetBytes(fileTable[i].filePath);
                Array.Copy(name, data, name.Length);
                BitConverter.GetBytes(Convert.ToInt32(fileTable[i].fileDataOffset)).CopyTo(data, 260);
                BitConverter.GetBytes(Convert.ToInt32(fileTable[i].fileDataDecompressedSize)).CopyTo(data, 264);
                BitConverter.GetBytes(Convert.ToInt32(fileTable[i].fileDataCompressedSize)).CopyTo(data, 268);

                MemoryStream ms = new MemoryStream();
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(1,false);
                Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms,defl);
                s.Write(data, 0, data.Length);
                s.Close();
                byte[] dataComp = (byte[])ms.ToArray();
                entrySize = (int)dataComp.Length;

                bw.Write(entrySize ^ KEY_1);
                bw.Write(entrySize ^ KEY_2);

                bw.Write(dataComp);

                //ShowProgress(entryCount, i+1);
            }

            bw.Write((int)-1526153788);
            bw.Write((short)2);
            bw.Write((short)2);
            bw.Write(fileTableOffset ^ KEY_1);
            bw.Write((int)0);
            bw.Write(System.Text.Encoding.GetEncoding(936).GetBytes("Angelica File Package, Perfect World."));
            for (int i = 0; i < 215; i++) {
                bw.Write((byte)0);
            }
            bw.Write((int)-2060097592);
            bw.Write((int)entryCount);
            bw.Write((short)2);
            bw.Write((short)2);
            fs.SetLength(fs.Position);
            int fileSize = (int)fs.Position;
            fs.Seek(4, SeekOrigin.Begin);
            bw.Write(fileSize);

            br.Close();
            bw.Close();
            fs.Close();

            //textBox1.Text += editedFiles.ToString()+" File(s) patched";
            //label4.Text = filesCount.ToString()+" file(s) patched";
        }
Ejemplo n.º 17
0
        void CompressPck()
        {
            string pckFile = textBox2.Text;

            string pckFolder = textBox3.Text + "\\";
            int pckFolderLength = pckFolder.Length;
            //string[] files = Directory.GetFiles("C:\\fw\\", "*.*", SearchOption.AllDirectories);
            string[] files = Directory.GetFiles(pckFolder, "*.*", SearchOption.AllDirectories);

            int entryCount = files.Length;
            int fileTableOffset;
            fileTableEntry[] fileTable = new fileTableEntry[entryCount];
            int entrySize;

            FileStream BinaryFile = new FileStream(pckFile, FileMode.Create, FileAccess.ReadWrite);
            BinaryWriter bw = new BinaryWriter(BinaryFile);
            bw.Write(1305093103);
            bw.Write((int)0); // placeholder for filesize
            bw.Write(1453361591);

            for (int i = 0; i < entryCount; i++) {

                fileTable[i].filePath = files[i].Substring(pckFolderLength);
                fileTable[i].fileDataOffset = (int)BinaryFile.Position;

                FileStream  fs = File.OpenRead(pckFolder+fileTable[i].filePath);
                BinaryReader br = new BinaryReader(fs);

                fileTable[i].fileDataDecompressedSize = (int)fs.Length;
                //textBox1.Text += "\r\n__"+fileTable[i].fileDataDecompressedSize.ToString();
                byte[] data = br.ReadBytes(fileTable[i].fileDataDecompressedSize);

                //byte[] bytData = System.Text.Encoding.UTF8.GetBytes(strInput);
                MemoryStream ms = new MemoryStream();
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(1,false);
                Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms,defl);
                s.Write(data, 0, data.Length);
                s.Close();
                byte[] dataComp = (byte[])ms.ToArray();

                fileTable[i].fileDataCompressedSize = (int)dataComp.Length;

                if(fileTable[i].fileDataCompressedSize < fileTable[i].fileDataDecompressedSize)
                {
                    bw.Write(dataComp);
                    if ( i == 0){
                        //textBox1.Text += "\r\n--"+BitConverter.ToString(dataComp);
                    }
                }
                // no zlib
                else
                {
                    bw.Write(data);
                }

                br.Close();
                fs.Close();

                for (int m = 0; m < 215; m++) {
                    bw.Write((byte)0);
                }

                ShowProgress(entryCount, i+1);

            }

            fileTableOffset = (int)BinaryFile.Position;

            for (int i = 0; i < entryCount; i++) {

                byte[] data = new byte[276];
                byte[] name = System.Text.Encoding.GetEncoding(936).GetBytes(fileTable[i].filePath);
                Array.Copy(name, data, name.Length);
                BitConverter.GetBytes(Convert.ToInt32(fileTable[i].fileDataOffset)).CopyTo(data, 260);
                BitConverter.GetBytes(Convert.ToInt32(fileTable[i].fileDataDecompressedSize)).CopyTo(data, 264);
                BitConverter.GetBytes(Convert.ToInt32(fileTable[i].fileDataCompressedSize)).CopyTo(data, 268);

                MemoryStream ms = new MemoryStream();
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(1,false);
                Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms,defl);
                s.Write(data, 0, data.Length);
                s.Close();
                byte[] dataComp = (byte[])ms.ToArray();
                if ( dataComp.Length >= data.Length )
                    dataComp = data;
                entrySize = (int)dataComp.Length;

                bw.Write(entrySize ^ KEY_1);
                bw.Write(entrySize ^ KEY_2);

                bw.Write(dataComp);

                ShowProgress(entryCount, i+1);
            }

            bw.Write((int)-1526153788);
            bw.Write((short)2);
            bw.Write((short)2);
            bw.Write(fileTableOffset ^ KEY_1);
            bw.Write((int)0);
            bw.Write(System.Text.Encoding.GetEncoding(936).GetBytes("Angelica File Package, Perfect World."));
            for (int i = 0; i < 215; i++) {
                bw.Write((byte)0);
            }
            bw.Write((int)-2060097592);
            bw.Write((int)entryCount);
            bw.Write((short)2);
            bw.Write((short)2);

            int fileSize = (int)BinaryFile.Position;
            BinaryFile.Seek(4, SeekOrigin.Begin);
            bw.Write(fileSize);

            //textBox1.Text += "\r\n__"+BinaryFile.Position.ToString();
            bw.Close();
            BinaryFile.Close();

            label4.Text = entryCount.ToString()+" file(s) compressed";
        }
Ejemplo n.º 18
0
        void Button3Click(object sender, EventArgs e)
        {
            byte[] data = System.Text.Encoding.GetEncoding(936).GetBytes("abcdefghijklmnopqrstyhdzayhdzaydhazudzaduvwxyz");
            int dataSize = data.Length;
            MemoryStream ms = new MemoryStream();
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9,false);
            Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms,defl);
            s.Write(data, 0, data.Length);
            s.Close();

            byte[] dataComp = (byte[])ms.ToArray();

            MemoryStream msb = new MemoryStream(dataComp,0,dataComp.Length);
            Stream s2 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(msb);
            byte[] dataDec = new byte[dataSize];
            s2.Read(dataDec,0,dataSize);

            string res = Encoding.GetEncoding(936).GetString(dataDec);
            textBox1.Text = dataComp.Length.ToString();
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Replaces a file by a file
        /// </summary>
        /// <param name="archFile">file to replace</param>
        /// <param name="newFile">new file</param>
        public void ReplaceFile(FileInArchive archFile, FileStream newFile)
        {
            byte[] newFileBuffer = new byte[newFile.Length];
            newFile.Read(newFileBuffer, 0, newFileBuffer.Length);

            MemoryStream inputMS = new MemoryStream(newFileBuffer);
            MemoryStream outputMS = new MemoryStream();

            byte[] output_buffer = new byte[0];

            if (archFile.descriptor.compressionMethod == 1 && false) //ZLib compression
            {
                try
                {
                    ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
                    ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream defstream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(outputMS, def);
                    defstream.Write(newFileBuffer, 0, newFileBuffer.Length);
                    defstream.Flush();
                    defstream.Finish();

                    output_buffer = outputMS.GetBuffer();
                }
                catch (Exception)
                {

                }

                archFile.descriptor.uncompressedSize = (uint)inputMS.Length;

                WriteFileToArchive(archFile, outputMS);
            }
            else if (archFile.descriptor.compressionMethod == 0 || true) //No compression
            {
                archFile.descriptor.compressionMethod = 0;
                CopyStream(inputMS, outputMS);

                archFile.descriptor.uncompressedSize = (uint)inputMS.Length;

                WriteFileToArchive(archFile, outputMS);
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Compresses File Content
        /// </summary>
        /// <param name="ds">File Content to be compressed.</param>
        /// <returns>Byte array.</returns>
        public static byte[] CompressFileContent(byte[] blobContent)
        {

            MemoryStream ms2 = new MemoryStream(blobContent);
            StreamReader reader = new StreamReader(ms2);
            MemoryStream ms3 = new MemoryStream();
            
            
            string str = reader.ReadToEnd(); 

            ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9 , false);
            ms2.Position = 0;
            Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms3, defl);
            
            
            s.Write(ms2.ToArray(), 0,(int) ms2.Length);
            s.Close();
            
            
            return (byte[])ms3.ToArray();

        }
Ejemplo n.º 21
0
		/// <summary>
		/// Compresses a DataSet.
		/// </summary>
		/// <param name="ds">DataSet to be compressed.</param>
		/// <param name="schemaOnly">If TRUE, compresses only the schema.</param>
		/// <param name="diffGram"></param>
		/// <returns>Byte array.</returns>
		private static byte[] CompressDataSet(DataSet ds, bool schemaOnly, bool diffGram)
		{
			SetNeutralDataTable(ds);
			MemoryStream ms2 = new MemoryStream();
			ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9, false);
			Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms2, defl);
			MemoryStream ms3 = new MemoryStream();
			if (schemaOnly)
			{
				ds.WriteXmlSchema(ms3);
			}
			else
			{
				ds.WriteXml(ms3, (diffGram) ? XmlWriteMode.DiffGram : XmlWriteMode.WriteSchema);
			}
			s.Write(ms3.ToArray(), 0, (int)ms3.Length);
			s.Close();
            
			return (byte[])ms2.ToArray();

		}