Write() public method

public Write ( byte array, int offset, int count ) : void
array byte
offset int
count int
return void
コード例 #1
0
        static void CompressFile(string sDir, string sRelativePath, GZipStream zipStream)
        {
            //Compress file name
            char[] chars = sRelativePath.ToCharArray();
            zipStream.Write(BitConverter.GetBytes(chars.Length), 0, sizeof(int));
            foreach (char c in chars)
                zipStream.Write(BitConverter.GetBytes(c), 0, sizeof(char));

            //Compress file content
            byte[] bytes = File.ReadAllBytes(Path.Combine(sDir, sRelativePath));
            zipStream.Write(BitConverter.GetBytes(bytes.Length), 0, sizeof(int));
            zipStream.Write(bytes, 0, bytes.Length);
        }
コード例 #2
0
ファイル: LvlFile.cs プロジェクト: tommyz56/MCGalaxy
        public static void Save(Level level, string file) {
            using (Stream fs = File.Create(file),
                   gs = new GZipStream(fs, CompressionMode.Compress, true))
            {
                byte[] header = new byte[16];
                BitConverter.GetBytes(1874).CopyTo(header, 0);
                gs.Write(header, 0, 2);

                BitConverter.GetBytes(level.Width).CopyTo(header, 0);
                BitConverter.GetBytes(level.Length).CopyTo(header, 2);
                BitConverter.GetBytes(level.Height).CopyTo(header, 4);
                BitConverter.GetBytes(level.spawnx).CopyTo(header, 6);
                BitConverter.GetBytes(level.spawnz).CopyTo(header, 8);
                BitConverter.GetBytes(level.spawny).CopyTo(header, 10);
                header[12] = level.rotx;
                header[13] = level.roty;
                header[14] = (byte)level.permissionvisit;
                header[15] = (byte)level.permissionbuild;
                gs.Write(header, 0, header.Length);
                byte[] blocks = level.blocks;
                byte[] convBlocks = new byte[blocks.Length];

                for (int i = 0; i < blocks.Length; ++i) {
                    byte block = blocks[i];
                    if (block < Block.CpeCount) {
                        convBlocks[i] = block; //CHANGED THIS TO INCOPARATE SOME MORE SPACE THAT I NEEDED FOR THE door_orange_air ETC.
                    } else {
                        convBlocks[i] = Block.SaveConvert(block);
                    }
                }
                gs.Write(convBlocks, 0, convBlocks.Length);
                
                // write out new blockdefinitions data
                gs.WriteByte(0xBD);
                int index = 0;
                for (int y = 0; y < level.ChunksY; y++)
                	for (int z = 0; z < level.ChunksZ; z++)
                		for (int x = 0; x < level.ChunksX; x++)
                {
                    byte[] chunk = level.CustomBlocks[index];
                    if (chunk == null) {
                        gs.WriteByte(0);
                    } else {
                        gs.WriteByte(1);
                        gs.Write(chunk, 0, chunk.Length);
                    }
                    index++;
                }
            }
        }
コード例 #3
0
        public static byte[] Compress(byte[] data, bool useGZipCompression = true)

        {
            System.IO.Compression.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Fastest;

            using (MemoryStream memoryStream = new MemoryStream())

            {
                if (useGZipCompression)

                {
                    using (System.IO.Compression.GZipStream gZipStream = new System.IO.Compression.GZipStream(memoryStream,

                                                                                                              compressionLevel, true))

                    {
                        gZipStream.Write(data, 0, data.Length);
                    }
                }

                else

                {
                    using (System.IO.Compression.GZipStream gZipStream = new System.IO.Compression.GZipStream(memoryStream,

                                                                                                              compressionLevel, true))

                    {
                        gZipStream.Write(data, 0, data.Length);
                    }
                }

                return(memoryStream.ToArray());
            }
        }
コード例 #4
0
ファイル: CryptoHelper.cs プロジェクト: hostitherepc/Fork-1
        public static Stream CompressStream(Stream inStream)
        {
            MemoryStream outStream = new System.IO.MemoryStream();

            inStream.Position = 0;

            using (GZipStream compressedStream = new GZipStream(outStream, CompressionMode.Compress, true))
            {

                // Read the in stream in 1024 byte chunks
                byte[] buffer = new byte[1024];
                int bytesRead = 0;

                do
                {
                    bytesRead = inStream.Read(buffer, 0, 1024);

                    if (bytesRead > 0)
                        compressedStream.Write(buffer, 0, bytesRead);
                }
                while (bytesRead > 0);
            }

            outStream.Seek(0, SeekOrigin.Begin);

            return outStream;
        }
コード例 #5
0
        public static string compress(this FileInfo fi, string targetFile)
        {
            targetFile.deleteIfExists();
            // Get the stream of the source file.
            using (FileStream inFile = fi.OpenRead())
            {
                // Prevent compressing hidden and already compressed files.
                if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
                        != FileAttributes.Hidden & fi.Extension != ".gz")
                {
                    // Create the compressed file.
                    using (FileStream outFile = File.Create(targetFile))
                    {
                        using (var compress = new GZipStream(outFile, CompressionMode.Compress))
                        {
                            // Copy the source file into the compression stream.
                            var buffer = new byte[4096];
                            int numRead;
                            while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
                                compress.Write(buffer, 0, numRead);

                            "Compressed {0} from {1} to {2} bytes.".info(fi.Name, fi.Length.str(), outFile.Length.str());
                        }
                    }
                }
            }
            if (targetFile.fileExists())
                return targetFile;
            return null;
        }
コード例 #6
0
        public static string Zip(string value)
        {
            //Transform string into byte[]
            byte[] byteArray = new byte[value.Length];
            int    indexBA   = 0;

            char[] charArray = value.ToCharArray();
            foreach (char item in charArray)
            {
                byteArray[indexBA++] = (byte)item;
            }

            //Prepare for compress
            System.IO.MemoryStream           ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
                                                                                       System.IO.Compression.CompressionMode.Compress);

            //Compress
            sw.Write(byteArray, 0, byteArray.Length);
            //Close, DO NOT FLUSH cause bytes will go missing...
            sw.Close();

            //Transform byte[] zip data to string
            byteArray = ms.ToArray();
            System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
            foreach (byte item in byteArray)
            {
                sB.Append((char)item);
            }
            ms.Close();
            sw.Dispose();
            ms.Dispose();
            return(sB.ToString());
        }
コード例 #7
0
ファイル: GzipUtils.cs プロジェクト: rongxiong/Scut
        /// <summary>
        /// 压缩流数据
        /// </summary>
        /// <param name="aSourceStream"></param>
        /// <returns></returns>
        public static byte[] EnCompress(Stream aSourceStream)
        {
            MemoryStream vMemory = new MemoryStream();

            aSourceStream.Seek(0, SeekOrigin.Begin);
            vMemory.Seek(0, SeekOrigin.Begin);
            try
            {
                using (GZipStream vZipStream = new GZipStream(vMemory, CompressionMode.Compress))
                {
                    byte[] vFileByte = new byte[1024];
                    int vRedLen = 0;
                    do
                    {
                        vRedLen = aSourceStream.Read(vFileByte, 0, vFileByte.Length);
                        vZipStream.Write(vFileByte, 0, vRedLen);
                    }
                    while (vRedLen > 0);
                }
            }
            finally
            {
                vMemory.Dispose();
            }
            return vMemory.ToArray();
        }
コード例 #8
0
        public void Configure(JsModelsConfiguration configuration)
        {
            // save path
            Path = configuration.Path;

            // compute js
            var generator = new JsModelGenerator(configuration.Models);
            _js = generator.GenerateModels(configuration.Models);

            // minify
            _js = (new Minifier()).MinifyJavaScript(_js);

            // get version hash
            var encoding = new UTF8Encoding();
            var bytes = encoding.GetBytes(_js);
            VersionHash = Convert.ToBase64String(SHA512.Create().ComputeHash(bytes));

            // compress and read out to byte array
            using (var ms = new MemoryStream())
            {
                using (var stream = new GZipStream(ms, CompressionLevel.Optimal, false))
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
                _jsCompressed = ms.ToArray();
            }
        }
コード例 #9
0
ファイル: GZip.cs プロジェクト: bluejack2000/core
 public static string Compress(string text)
 {
     // convert text to bytes
     byte[] buffer = Encoding.UTF8.GetBytes(text);
     // get a stream
     MemoryStream ms = new MemoryStream();
     // get ready to zip up our stream
     using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
     {
         // compress the data into our buffer
         zip.Write(buffer, 0, buffer.Length);
     }
     // reset our position in compressed stream to the start
     ms.Position = 0;
     // get the compressed data
     byte[] compressed = ms.ToArray();
     ms.Read(compressed, 0, compressed.Length);
     // prepare final data with header that indicates length
     byte[] gzBuffer = new byte[compressed.Length + 4];
     //copy compressed data 4 bytes from start of final header
     System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
     // copy header to first 4 bytes
     System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
     // convert back to string and return
     return Convert.ToBase64String(gzBuffer);
 }
コード例 #10
0
ファイル: GZip.cs プロジェクト: umby24/Hypercube
        /// <summary>
        /// GZip Compresses a file at the given file path.
        /// </summary>
        /// <param name="filepath">The path to the file to compress with gzip.</param>
        public static void CompressFile(string filepath)
        {
            if (!File.Exists(filepath))
                return;

            const int chunkSize = 65536;

            try {
                using (var fs = new FileStream(filepath, FileMode.Open)) {
                    using (var gs = new GZipStream(new FileStream("Temp.gz", FileMode.Create), CompressionMode.Compress)) {
                        var buffer = new byte[chunkSize];

                        while (true) {
                            var bytesRead = fs.Read(buffer, 0, chunkSize);

                            if (bytesRead == 0) break;

                            gs.Write(buffer, 0, bytesRead);
                        }
                    }
                }

                File.Delete(filepath);
                File.Move("Temp.gz", filepath);
            } catch {
                GC.Collect();
            }
        }
コード例 #11
0
ファイル: StringExtensions.cs プロジェクト: popapps/SPAtube
        public static byte[] Compress(this string value)
        {
            //Transform string into byte[]
            var byteArray = new byte[value.Length];
            var index = 0;
            foreach (char item in value)
            {
                byteArray[index++] = (byte)item;
            }

            //Prepare for compress
            using (var ms = new MemoryStream())
            {
                using (var sw = new GZipStream(ms, CompressionMode.Compress))
                {

                    //Compress
                    sw.Write(byteArray, 0, byteArray.Length);
                    //Close, DO NOT FLUSH cause bytes will go missing...
                }
                //Transform byte[] zip data to string
                byteArray = ms.ToArray();
            }
            return byteArray;
        }
コード例 #12
0
ファイル: StudyXmlIo.cs プロジェクト: scottshea/monodicom
		public static void WriteGzip(XmlDocument theDoc, Stream theStream)
		{
			MemoryStream ms = new MemoryStream();
			XmlWriterSettings xmlSettings = new XmlWriterSettings();

			xmlSettings.Encoding = Encoding.UTF8;
			xmlSettings.ConformanceLevel = ConformanceLevel.Document;
			xmlSettings.Indent = false;
			xmlSettings.NewLineOnAttributes = false;
			xmlSettings.CheckCharacters = true;
			xmlSettings.IndentChars = "";

			XmlWriter tw = XmlWriter.Create(ms, xmlSettings);

			theDoc.WriteTo(tw);
			tw.Flush();
			tw.Close();

			byte[] buffer = ms.GetBuffer();

			GZipStream compressedzipStream = new GZipStream(theStream, CompressionMode.Compress, true);
			compressedzipStream.Write(buffer, 0, buffer.Length);
			// Close the stream.
			compressedzipStream.Flush();
			compressedzipStream.Close();

			// Force a flush
			theStream.Flush();
		}
コード例 #13
0
 /// <inheritdoc />
 public byte[] Compress(byte[] data, string dataName)
 {
     var output = new MemoryStream();
     using (var stream = new GZipStream(output, CompressionLevel.Optimal))
         stream.Write(data, 0, data.Length);
     return output.ToArray();
 }
コード例 #14
0
ファイル: Compress.cs プロジェクト: versidyne/cresco
        public void FileCompress(string sourceFile, string destinationFile)
        {
            // ----- Decompress a previously compressed string.
            //       First, create the input file stream.
            FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);

            // ----- Create the output file stream.
            FileStream destinationStream = new FileStream(destinationFile, FileMode.Create, FileAccess.Write);

            // ----- Bytes will be processed by a compression
            //       stream.
            GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true);

            // ----- Process bytes from one file into the other.
            const int BlockSize = 4096;
            byte[] buffer = new byte[BlockSize + 1];
            int bytesRead = default(int);
            do
            {
                bytesRead = sourceStream.Read(buffer, 0, BlockSize);
                if (bytesRead == 0)
                {
                    break;
                }
                compressedStream.Write(buffer, 0, bytesRead);
            } while (true);

            // ----- Close all the streams.
            sourceStream.Close();
            compressedStream.Close();
            destinationStream.Close();
        }
コード例 #15
0
ファイル: CompressHelper.cs プロジェクト: notinmood/hiland
        /// <summary>
        /// 对byte数组进行压缩
        /// </summary>
        /// <param name="data">待压缩的byte数组</param>
        /// <returns>压缩后的byte数组</returns>
        public static byte[] Compress(byte[] data)
        {
            try
            {
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
                    {
                        zip.Write(data, 0, data.Length);
                        zip.Close();
                    }
                    buffer = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(buffer, 0, buffer.Length);
                    ms.Close();
                }
                return buffer;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
コード例 #16
0
        static void Slice(string sourceFile, string destinationDirectory, int parts)
        {
            FileInfo fileInfo = new FileInfo(sourceFile);
            string extension = fileInfo.Extension;
            long bytesToCut = fileInfo.Length / parts;

            using (FileStream fileStream = new FileStream(sourceFile, FileMode.Open))
            {
                byte[] buffer = new byte[bytesToCut];

                for (int i = 1; i <= parts; i++)
                {
                    using (FileStream partFileStream = new FileStream(string.Format("{0}{1}{2}{3}{4}", destinationDirectory, "Part ", i, extension, ".gz"), FileMode.Create))
                    {
                        using(GZipStream zip = new GZipStream(partFileStream, CompressionLevel.Fastest, false))
                        {
                            int lengthRead = fileStream.Read(buffer, 0, buffer.Length);

                            zip.Write(buffer, 0, lengthRead);

                        }

                    }

                }

            }
        }
コード例 #17
0
 /// <summary>
 /// Compresses an Epi Info 7 data package.
 /// </summary>
 /// <param name="fi">The file info for the raw data file that will be compressed.</param>
 public static void CompressDataPackage(FileInfo fi)
 {
     using (FileStream inFile = fi.OpenRead())
     {
         // Prevent compressing hidden and already compressed files.
         if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
                 != FileAttributes.Hidden & fi.Extension != ".gz")
         {
             using (FileStream outFile = File.Create(fi.FullName + ".gz"))
             {
                 using (GZipStream Compress = new GZipStream(outFile,
                         CompressionMode.Compress))
                 {
                     // Copy the source file into the compression stream.
                     byte[] buffer = new byte[4096];
                     int numRead;
                     while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
                     {
                         Compress.Write(buffer, 0, numRead);
                     }
                     Compress.Close();
                     Compress.Dispose();
                 }
             }
         }
     }
 }
コード例 #18
0
ファイル: Compressor.cs プロジェクト: hultqvist/scatter
        static void CompressFile(string path)
        {
            DateTime fileDate = File.GetLastWriteTime(path);

            //Minify uncompressed file
            if (path.EndsWith(".js"))
            {
                string original = File.ReadAllText(path, Encoding.UTF8);
                if (string.IsNullOrEmpty(original))
                    return;
                string compressed = jc.Compress(original);
                File.WriteAllText(path, compressed, Encoding.UTF8);
                File.SetLastWriteTime(path, fileDate);
            }
            if (path.EndsWith(".css"))
            {
                string original = File.ReadAllText(path, Encoding.UTF8);
                if (string.IsNullOrEmpty(original))
                    return;
                string compressed = cc.Compress(original);
                File.WriteAllText(path, compressed, Encoding.UTF8);
                File.SetLastWriteTime(path, fileDate);
            }

            //Compress
            using (GZipStream gz = new GZipStream(new FileStream(path + ".gz", FileMode.Create), CompressionMode.Compress))
            {
                byte[] buffer = File.ReadAllBytes(path);
                gz.Write(buffer, 0, buffer.Length);
            }

            File.SetLastWriteTime(path + ".gz", fileDate);
        }
コード例 #19
0
		public static string Zip(string value)
		{
			byte[] buffer = Encoding.UTF8.GetBytes(value);
			var memoryStream = new MemoryStream();
			using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
			{
				gZipStream.Write(buffer, 0, buffer.Length);
			}

			memoryStream.Position = 0;

			var compressedData = new byte[memoryStream.Length];
			memoryStream.Read(compressedData, 0, compressedData.Length);

			var gZipBuffer = new byte[compressedData.Length + 4];
			Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
			Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);

			//Transform byte[] unzip data to string
			System.Text.StringBuilder sB = new System.Text.StringBuilder(gZipBuffer.Length);
			//Read the number of bytes GZipStream red and do not a for each bytes in
			//resultByteArray;
			for (int i = 0; i < gZipBuffer.Length; i++)
			{
				sB.Append((char)gZipBuffer[i]);
			}

			return sB.ToString();
		}
コード例 #20
0
        private static void SliceGZip(string sourceFile, string destinationDir, int numParts)
        {
            FileInfo fileInfo = new FileInfo(sourceFile);

            int size = (int)fileInfo.Length; //size in bytes
            int partSize = size / numParts;
            int lastPartSize = size - (numParts - 1) * partSize;

            string name = fileInfo.Name;
            string[] nameExtArr = name.Split('.');

            //for each part, read input and write output
            FileStream source = new FileStream(sourceFile, FileMode.Open);
            source.Seek(0, SeekOrigin.Current);
            for (int i = 0; i < numParts; i++)
            {
            int currentSize = (i == (numParts - 1)) ? lastPartSize : partSize;

            byte[] buffer = new byte[currentSize];
            source.Read(buffer, 0, currentSize);

            string currentFilePath = destinationDir + "Part-" + i + "." + nameExtArr[1]+".gz";
            using (FileStream dest = new FileStream(currentFilePath, FileMode.Create))
                using (GZipStream gzipDest = new GZipStream(dest, CompressionMode.Compress, false))
                    gzipDest.Write(buffer, 0, currentSize);

            }
            source.Close();
        }
コード例 #21
0
ファイル: DataEncryption.cs プロジェクト: FTCEEP/CGUST
        public byte[] DataSetToByte(DataSet ds)
        {
            try
            {
                ds.RemotingFormat = SerializationFormat.Binary;
                BinaryFormatter ser = new BinaryFormatter();
                MemoryStream unMS = new MemoryStream();
                ser.Serialize(unMS, ds);

                byte[] bytes = unMS.ToArray();
                int lenbyte = bytes.Length;

                MemoryStream compMs = new MemoryStream();
                GZipStream compStream = new GZipStream(compMs, CompressionMode.Compress, true);
                compStream.Write(bytes, 0, lenbyte);

                compStream.Close();
                unMS.Close();
                compMs.Close();
                byte[] zipData = compMs.ToArray();

                return zipData;
            }
            catch (Exception ex)
            {
                gLogger.ErrorException("DataSetToByte--", ex);
                throw ex;
            }
        }
コード例 #22
0
ファイル: ZipHelper.cs プロジェクト: vanloc0301/mychongchong
 public static byte[] GZCompress(byte[] source)
 {
     byte[] buffer;
     if ((source == null) || (source.Length == 0))
     {
         throw new ArgumentNullException("source");
     }
     try
     {
         using (MemoryStream stream = new MemoryStream())
         {
             using (GZipStream stream2 = new GZipStream(stream, CompressionMode.Compress, true))
             {
                 Console.WriteLine("Compression");
                 stream2.Write(source, 0, source.Length);
                 stream2.Flush();
                 stream2.Close();
                 Console.WriteLine("Original size: {0}, Compressed size: {1}", source.Length, stream.Length);
                 stream.Position = 0L;
                 buffer = stream.ToArray();
             }
         }
     }
     catch (Exception exception)
     {
         LoggingService.Error("GZip压缩时出错:", exception);
         buffer = source;
     }
     return buffer;
 }
コード例 #23
0
ファイル: MailMgr.cs プロジェクト: mynew4/DAoC
		/// <summary>
		/// Compresses a file with the gzip algorithm.
		/// (If you want to keep the correct file name in the archive you must set the dest
		/// path value equals to source + ".gz")
		/// </summary>
		/// <param name="source">The source file path.</param>
		/// <param name="dest">The destination file path.</param>
		/// <returns>True if the file have been successfully compressed.</returns>
		public static bool compressFile(string source, string dest)
		{
			try
			{
				// Stream to read the file
				FileStream fstream = new FileStream(source, FileMode.Open, FileAccess.Read);

				// We store the complete file into a buffer
				byte[] buf = new byte[fstream.Length];
				fstream.Read(buf, 0, buf.Length);
				fstream.Close();

				// Stream to write the compressed file
				fstream = new FileStream(dest, FileMode.Create);

				// File compression (fstream is automatically closed)
				GZipStream zipStream = new GZipStream(fstream, CompressionMode.Compress, false);
				zipStream.Write(buf, 0, buf.Length);
				zipStream.Close();

				return true;
			}
			catch (Exception e)
			{
				MailMgr.Logger.Error(e);
				return false;
			}
		}
コード例 #24
0
        /// <summary />
        internal static byte[] ZipBytes(this byte[] byteArray, bool fast = true)
        {
            if (byteArray == null)
            {
                return(null);
            }
            try
            {
                //Prepare for compress
                using (var ms = new System.IO.MemoryStream())
                {
                    var level = System.IO.Compression.CompressionLevel.Fastest;
                    if (!fast)
                    {
                        level = System.IO.Compression.CompressionLevel.Optimal;
                    }
                    using (var sw = new System.IO.Compression.GZipStream(ms, level))
                    {
                        //Compress
                        sw.Write(byteArray, 0, byteArray.Length);
                        sw.Close();

                        //Transform byte[] zip data to string
                        return(ms.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
コード例 #25
0
        public void Run()
        {
            string folder = @"c:\temp";
            string uncompressedFilePath = Path.Combine(folder, "uncompressed.dat");
            string compressedFilePath = Path.Combine(folder, "compressed.gz");
            byte[] dataToCompress = Enumerable.Repeat((byte) 'a', 1024*1024).ToArray();

            using (FileStream uncompressedFileStream = File.Create(uncompressedFilePath))
            {
                uncompressedFileStream.Write(dataToCompress, 0, dataToCompress.Length);
            }
            using (FileStream compressedFileStream = File.Create(compressedFilePath))
            {
                using (var compressionStream = new GZipStream(
                    compressedFileStream, CompressionMode.Compress))
                {
                    compressionStream.Write(dataToCompress, 0, dataToCompress.Length);
                }
            }

            var uncompressedFile = new FileInfo(uncompressedFilePath);
            var compressedFile = new FileInfo(compressedFilePath);

            Console.WriteLine(uncompressedFile.Length);
            Console.WriteLine(compressedFile.Length);
        }
コード例 #26
0
        public static void Compress(string FileToCompress, string CompressedFile)
        {
            byte[] buffer = new byte[1024 * 1024]; // 1MB

            using (System.IO.FileStream sourceFile = System.IO.File.OpenRead(FileToCompress))
            {
                using (System.IO.FileStream destinationFile = System.IO.File.Create(CompressedFile))
                {
                    using (System.IO.Compression.GZipStream output = new System.IO.Compression.GZipStream(destinationFile,
                                                                                                          System.IO.Compression.CompressionMode.Compress))
                    {
                        int bytesRead = 0;
                        while (bytesRead < sourceFile.Length)
                        {
                            int ReadLength = sourceFile.Read(buffer, 0, buffer.Length);
                            output.Write(buffer, 0, ReadLength);
                            output.Flush();
                            bytesRead += ReadLength;
                        } // Whend

                        destinationFile.Flush();
                    } // End Using System.IO.Compression.GZipStream output

                    destinationFile.Close();
                } // End Using System.IO.FileStream destinationFile

                // Close the files.
                sourceFile.Close();
            } // End Using System.IO.FileStream sourceFile
        }     // End Sub CompressFile
コード例 #27
0
ファイル: CompressionUtils.cs プロジェクト: jemc771/MongoREST
 public static string Compress(string text)
 {
   try
   {
     byte[] buffer = Encoding.UTF8.GetBytes(text);
     using (MemoryStream ms = new MemoryStream())
     {
       using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
       {
         zip.Write(buffer, 0, buffer.Length);
       }
       ms.Position = 0;
       using (MemoryStream outStream = new MemoryStream())
       {
         byte[] compressed = new byte[ms.Length];
         ms.Read(compressed, 0, compressed.Length);
         byte[] gzBuffer = new byte[compressed.Length + 4];
         System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
         System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
         string final = Convert.ToBase64String(gzBuffer);
         return final;
       }
     }
   }
   catch (Exception ex)
   {
     return ex.Message;
   }
 }
コード例 #28
0
ファイル: Compression.cs プロジェクト: DrPrickett/GIT
        public string CompressString(string text)
        {
            try
            {
                byte[]       buffer = System.Text.Encoding.Unicode.GetBytes(text);
                MemoryStream ms     = new MemoryStream();
                using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true))
                {
                    zip.Write(buffer, 0, buffer.Length);
                }

                ms.Position = 0;
                MemoryStream outStream = new MemoryStream();

                byte[] compressed = new byte[ms.Length];
                ms.Read(compressed, 0, compressed.Length);

                byte[] gzBuffer = new byte[compressed.Length + 4];

                System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
                System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
                return(Convert.ToBase64String(gzBuffer));
            }
            catch (Exception err)
            {
                return(err.Message.ToString());
            }
        }
コード例 #29
0
ファイル: Responder.cs プロジェクト: tekhedd/Interspecific
        /// <summary>
        /// Flushes the remaining contents from the buffer, sets the content length and closes the response output stream
        /// </summary>
        protected void FlushResponse(HttpListenerContext context, byte[] buffer, int length)
        {
            if (length > 1024
                && context.Request.Headers.AllKeys.Contains("Accept-Encoding")
                && context.Request.Headers["Accept-Encoding"].Contains("gzip"))
            {
                using (var ms = new MemoryStream())
                {
                    using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
                    {
                        zip.Write(buffer, 0, length);
                    }

                    ms.Position = 0;

                    context.Response.AddHeader("Content-Encoding", "gzip");
                    context.Response.ContentLength64 = ms.Length;
                    ms.WriteTo( context.Response.OutputStream );
                 }
            }
            else
            {
                context.Response.ContentLength64 = length;
                context.Response.OutputStream.Write(buffer, 0, length);
            }

            context.Response.OutputStream.Close();
            context.Response.Close();
        }
コード例 #30
0
    private IEnumerator DoUploadData()
    {
        using (var inputStream = new MemoryStream())
        {
            var data = Encoding.UTF8.GetBytes(dataCache);
            var gZip = new System.IO.Compression.GZipStream(inputStream, System.IO.Compression.CompressionMode.Compress);
            gZip.Write(data, 0, data.Length);
            gZip.Close();

            var toUploadData = Convert.ToBase64String(inputStream.ToArray());
            if (null != toUploadData && toUploadData.Length > 0)
            {
                using (var request = UnityEngine.Networking.UnityWebRequest.Put(serverUrl, toUploadData))
                {
                    yield return(request.SendWebRequest());

                    if (request.isHttpError || request.isNetworkError)
                    {
                        LogUtil.DebugLog("send data error: " + request.error);
                    }
                    else
                    {
                        LogUtil.DebugLog("send data success: " + dataCache);
                        dataCache = "";
                    }
                }
            }

            gZip.Dispose();
        }
    }
コード例 #31
0
        public void Memory_Compress_Decompress() {
            var input = (byte[])PlainBytes.Clone();
            byte[] compressed;

            using(var outStream = new MemoryStream(input.Length)) {
                using(var gzip = new GZipStream(outStream, CompressionMode.Compress)) {
                    gzip.Write(input, 0, input.Length);
                }
                compressed = outStream.ToArray();
                Assert.IsNotNull(compressed);
                Assert.IsTrue(compressed.Length > 0);
            }

            using(var outStream = new MemoryStream(input.Length))
            using(var gzip = new GZipStream(new MemoryStream(compressed), CompressionMode.Decompress)) {
                var readCount = 0;
                var buffer = new byte[CompressorTool.BUFFER_SIZE];

                while((readCount = gzip.Read(buffer, 0, buffer.Length)) > 0) {
                    outStream.Write(buffer, 0, readCount);
                }

                byte[] decompressed = outStream.ToArray();

                Assert.IsTrue(decompressed.Length > 0);
                Assert.AreEqual(PlainBytes, decompressed);
            }
        }
        public override void Load()
        {
            // depend on HiddenFieldPageStatePersister for heavy lifting and crypto
            base.Load();

            CompressedSerializedData compressedData = ViewState as CompressedSerializedData;
            if (compressedData == null && ControlState != null)
            {
                // the underlying data was not compressed
                return;
            }

            // decompress
            using (MemoryStream uncompressedStream = new MemoryStream())
            {
                using (GZipStream zipStream = new GZipStream(uncompressedStream, CompressionMode.Decompress, leaveOpen: true))
                {
                    zipStream.Write(compressedData.RawData, 0, compressedData.RawData.Length);
                }

                uncompressedStream.Position = 0;
                ObjectStateFormatter formatter = new ObjectStateFormatter();
                Pair pair = (Pair)formatter.Deserialize(uncompressedStream);

                // extract
                ViewState = pair.First;
                ControlState = pair.Second;
            }
        }
        public override void Save()
        {
            using (MemoryStream uncompressedStream = new MemoryStream())
            {
                ObjectStateFormatter formatter = new ObjectStateFormatter();
                formatter.Serialize(uncompressedStream, new Pair(ViewState, ControlState));

                using (MemoryStream compressedStream = new MemoryStream())
                {
                    using (GZipStream zipStream = new GZipStream(compressedStream, CompressionLevel.Optimal, leaveOpen: true))
                    {
                        zipStream.Write(uncompressedStream.GetBuffer(), 0, checked((int)uncompressedStream.Length));
                    }

                    if (uncompressedStream.Length > compressedStream.Length)
                    {
                        // compressing will probably save space
                        // CompressedSerializeData uses BinaryFormatter, which ObjectStateFormatter serializes better than byte[]
                        ViewState = new CompressedSerializedData() { RawData = compressedStream.ToArray() };
                        ControlState = null;
                    }

                    // depend on HiddenFieldPageStatePersister for heavy lifting and crypto
                    base.Save();
                }
            }
        }
コード例 #34
0
        public void FileAsync_Compress_Decompress() {
            var filename = FileTool.GetTempFileName();

            using(var fs = FileAsync.OpenWrite(filename))
            using(var gzip = new GZipStream(fs, CompressionMode.Compress)) {
                gzip.Write(PlainBytes, 0, PlainBytes.Length);
            }

            var fi = new FileInfo(filename);
            Assert.IsTrue(fi.Exists);
            Assert.IsTrue(PlainBytes.Length > fi.Length);


            var outStream = new MemoryStream((int)fi.Length * 2);
            using(var fs = FileAsync.OpenRead(filename))
            using(var gzip = new GZipStream(fs, CompressionMode.Decompress, true)) {
                var readCount = 0;
                var buffer = new byte[CompressorTool.BUFFER_SIZE];

                while((readCount = gzip.Read(buffer, 0, buffer.Length)) > 0) {
                    outStream.Write(buffer, 0, readCount);
                }

                var output = outStream.ToArray();
                Assert.AreEqual(PlainBytes.Length, output.Length);
                Assert.AreEqual(PlainBytes, output);
            }

            fi = new FileInfo(filename);
            fi.Delete();
        }
コード例 #35
0
        public string CompressHandHistory(string fullHandText)
        {
            if (fullHandText == null) return null;

            //Transform string into byte[]
            byte[] byteArray = new byte[fullHandText.Length];
            int indexBA = 0;
            foreach (char item in fullHandText.ToCharArray())
            {
                byteArray[indexBA++] = (byte)item;
            }

            //Prepare for compress
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Compress);

            //Compress
            sw.Write(byteArray, 0, byteArray.Length);
            //Close, DO NOT FLUSH cause bytes will go missing...
            sw.Close();

            //Transform byte[] zip data to string
            byteArray = ms.ToArray();
            System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
            foreach (byte item in byteArray)
            {
                sB.Append((char)item);
            }
            ms.Close();
            sw.Dispose();
            ms.Dispose();
            return sB.ToString();
        }
コード例 #36
0
ファイル: ZipHelper.cs プロジェクト: franknew/SOAFramework
 public static byte[] ZipToByte(string value, Encoding encode, bool useGzip = false)
 {
     byte[] byteArray = encode.GetBytes(value);
     byte[] data      = null;
     //Prepare for compress
     System.IO.MemoryStream ms = new System.IO.MemoryStream();
     if (useGzip)
     {
         System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress);
         //Compress
         sw.Write(byteArray, 0, byteArray.Length);
         //Close, DO NOT FLUSH cause bytes will go missing...
         sw.Close();
         sw.Dispose();
     }
     else
     {
         ms.Write(byteArray, 0, byteArray.Length);
     }
     data = ms.ToArray();
     //Transform byte[] zip data to string
     ms.Close();
     ms.Dispose();
     return(data);
 }
コード例 #37
0
ファイル: Program.cs プロジェクト: jbijoux/Exam70_483
        static void Main(string[] args)
        {
            string folder = "folder";
            string uncompressedFilePath = Path.Combine(folder, "uncompressed.dat");
            string compressedFilePath = Path.Combine(folder, "compressed.dat");
            CleanUp(uncompressedFilePath);
            CleanUp(compressedFilePath);
            Directory.CreateDirectory(folder);
            byte[] dataToCompress = Enumerable.Repeat((byte)'a', 1024 * 1024).ToArray();

            using (FileStream uncompressedFileStream = File.Create(uncompressedFilePath))
            {
                uncompressedFileStream.Write(dataToCompress, 0, dataToCompress.Length);
            }

            using (FileStream compressedFileStream = File.Create(compressedFilePath))
            {
                using (GZipStream cmpressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                {
                    cmpressionStream.Write(dataToCompress, 0, dataToCompress.Length);
                }
            }

            FileInfo uncompressedFile = new FileInfo(uncompressedFilePath);
            FileInfo compressedFile = new FileInfo(compressedFilePath);

            Console.WriteLine("uncompressedFile.Length = {0}", uncompressedFile.Length);
            Console.WriteLine("compressedFile.Length = {0}", compressedFile.Length);

            Console.Write("Press a key to exit ... ");
            Console.ReadKey();
        }
コード例 #38
0
 /// <summary>
 /// 压缩数据组
 /// </summary>
 /// <param name="source"></param>
 /// <returns></returns>
 public static byte[] Compress(this byte[] source)
 {
     using (var ms = new System.IO.MemoryStream())
         using (var gzip = new System.IO.Compression.GZipStream(ms, CompressionMode.Compress))
         {
             gzip.Write(source);
             gzip.Close();
             return(ms.ToArray());
         }
 }
コード例 #39
0
    static public byte[] CompressGzip(byte[] original)
    {
        using var compressedStream = new System.IO.MemoryStream();

        using var compressStream = new System.IO.Compression.GZipStream(
                  compressedStream, System.IO.Compression.CompressionMode.Compress);

        compressStream.Write(original);
        compressStream.Flush();
        return(compressedStream.ToArray());
    }
コード例 #40
0
        public static Byte[] GZipJsonString(string jsonBody)
        {
            string inputStr = jsonBody;

            byte[] inputBytes = Encoding.UTF8.GetBytes(inputStr);
            using (var outputStream = new MemoryStream())
            {
                using (var gZipStream = new System.IO.Compression.GZipStream(outputStream, System.IO.Compression.CompressionMode.Compress))
                    gZipStream.Write(inputBytes, 0, inputBytes.Length);
                return(inputBytes);
            }
        }
コード例 #41
0
    public static byte[] GZipCompress(byte[] ts)
    {
        byte[]       compressedbuffer = null;
        MemoryStream ms = new MemoryStream();

        using (System.IO.Compression.GZipStream zs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true))
        {
            zs.Write(ts, 0, ts.Length);
        }
        compressedbuffer = ms.ToArray();
        return(compressedbuffer);
    }
コード例 #42
0
ファイル: Cipher_Gzip.cs プロジェクト: Violet-Liu/Learn
 /// <summary>
 /// gzip压缩
 /// </summary>
 /// <param name="inputBytes"></param>
 /// <returns></returns>
 public static byte[] Compress(byte[] inputBytes)
 {
     using (MemoryStream ms = new MemoryStream(inputBytes.Length))
     {
         using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress))
         {
             gzip.Write(inputBytes, 0, inputBytes.Length);
             gzip.Flush();
             gzip.Close();
         }
         return(ms.ToArray());
     }
 }
コード例 #43
0
ファイル: CodeBuilder.cs プロジェクト: wwwK/EntityDB
 static byte[] GZip(byte[] byteArray)
 {
     using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
     {
         System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress);
         //Compress
         sw.Write(byteArray, 0, byteArray.Length);
         //Close, DO NOT FLUSH cause bytes will go missing...
         sw.Close();
         //Transform byte[] zip data to string
         return(ms.ToArray());
     }
 }
コード例 #44
0
ファイル: program.cs プロジェクト: inwenis/stream_funs
 static void GzipStream()
 {
     System.IO.FileStream             newFileStream = new System.IO.FileStream("compressed.gz", System.IO.FileMode.Create);
     System.IO.Compression.GZipStream gZipStream    =
         new System.IO.Compression.GZipStream(newFileStream, System.IO.Compression.CompressionLevel.Fastest);
     byte[] bytes = System.Text.Encoding.Unicode.GetBytes("This is some text that will be compressed");
     gZipStream.Write(bytes, 0, bytes.Length);
     gZipStream.Dispose();
     System.IO.Compression.GZipStream decompressStream =
         new System.IO.Compression.GZipStream(System.IO.File.OpenRead("compressed.gz"), System.IO.Compression.CompressionMode.Decompress);
     System.IO.StreamReader streamReader = new System.IO.StreamReader(decompressStream, System.Text.Encoding.Unicode);
     System.Console.WriteLine(streamReader.ReadToEnd());
 }
コード例 #45
0
ファイル: GelfTransport.cs プロジェクト: stefanoric/gelf4net
        protected byte[] GzipMessage(String message)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(message);
            var    ms     = new MemoryStream();

            using (var zip = new System.IO.Compression.GZipStream(ms, CompressionMode.Compress, true))
            {
                zip.Write(buffer, 0, buffer.Length);
            }
            ms.Position = 0;
            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);
            return(compressed);
        }
コード例 #46
0
        private static void CreateZipFromText(string text, string path)
        {
            byte[]     byteArray   = ASCIIEncoding.ASCII.GetBytes(text);
            string     encodedText = Convert.ToBase64String(byteArray);
            FileStream destFile    = System.IO.File.Create(path);

            byte[]       buffer       = Encoding.UTF8.GetBytes(encodedText);
            MemoryStream memoryStream = new MemoryStream();

            using (System.IO.Compression.GZipStream gZipStream = new System.IO.Compression.GZipStream(destFile, System.IO.Compression.CompressionMode.Compress, true))
            {
                gZipStream.Write(buffer, 0, buffer.Length);
            }
        }
コード例 #47
0
ファイル: Main.cs プロジェクト: PikachuCN/DnspodUpdate
        private void SaveConfig()
        {
            MemoryStream    ms        = new MemoryStream();
            BinaryFormatter binFormat = new BinaryFormatter();

            binFormat.Serialize(ms, config);
            byte[]     SerializeByte    = ms.ToArray();
            FileStream fs               = new FileStream(@".\config.bin", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            GZipStream compressedStream = new System.IO.Compression.GZipStream(fs, CompressionMode.Compress, true);

            compressedStream.Write(SerializeByte, 0, SerializeByte.Length);
            compressedStream.Flush();
            compressedStream.Close();
            fs.Close();
        }
コード例 #48
0
        static public void CompressFile(string sourceFile, string destinationFile)
        {
            int checkCounter;

            if (System.IO.File.Exists(sourceFile) == false)
            {
                return;
            }

            byte[] buffer;
            System.IO.FileStream sourceStream      = null;
            System.IO.FileStream destinationStream = null;
            //System.IO.Compression.DeflateStream compressedStream = null;
            System.IO.Compression.GZipStream compressedStream = null;

            try
            {
                sourceStream = new System.IO.FileStream(sourceFile, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read);
                buffer       = new byte[Convert.ToInt64(sourceStream.Length)];
                checkCounter = sourceStream.Read(buffer, 0, buffer.Length);

                //output (ZIP) file name
                destinationStream = new System.IO.FileStream(destinationFile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);

                // Create a compression stream pointing to the destiantion stream
                compressedStream = new System.IO.Compression.GZipStream(destinationStream, System.IO.Compression.CompressionMode.Compress, true);
                compressedStream.Write(buffer, 0, buffer.Length);
            }
            finally
            {
                // Make sure we allways close all streams
                if (sourceStream != null)
                {
                    sourceStream.Close();
                }
                if (compressedStream != null)
                {
                    compressedStream.Close();
                }

                if (destinationStream != null)
                {
                    destinationStream.Close();
                }
            }
        }
コード例 #49
0
        public void testGzipStream()
        {
            // make sure compression works, file should be smaller
            string sample = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "files", "fp.log");

            byte[] uncompressed = File.ReadAllBytes(sample);
            int    before       = uncompressed.Length;

            byte[] compressed;
            int    after = 0;

            // test gzip stream compression code
            using (MemoryStream compressStream = new MemoryStream())
                using (ZopfliStream compressor = new ZopfliStream(compressStream, ZopfliFormat.ZOPFLI_FORMAT_GZIP))
                {
                    compressor.Write(uncompressed, 0, before);
                    compressor.Close();
                    compressed = compressStream.ToArray();
                    after      = compressed.Length;
                }

            before.Should().BeGreaterThan(after);

            // make sure we can decompress the file using built-in .net
            byte[] decompressedBytes = new byte[before];
            using (MemoryStream tempStream = new MemoryStream(compressed))
                using (System.IO.Compression.GZipStream decompressionStream = new System.IO.Compression.GZipStream(tempStream, System.IO.Compression.CompressionMode.Decompress))
                {
                    decompressionStream.Read(decompressedBytes, 0, before);
                }
            uncompressed.Should().Equal(decompressedBytes);

            // use built-in .net compression and make sure zopfil is smaller
            int after_builtin = 0;

            using (MemoryStream compressStream = new MemoryStream())
                using (System.IO.Compression.GZipStream compressor = new System.IO.Compression.GZipStream(compressStream, System.IO.Compression.CompressionLevel.Optimal))
                {
                    compressor.Write(uncompressed, 0, before);
                    compressor.Close();
                    after_builtin = compressStream.ToArray().Length;
                }

            after_builtin.Should().BeGreaterThan(after);
        }
コード例 #50
0
 public static string Compress(string s)
 {
     if (GameState.UseNewSaveCompression)
     {
         return(Compress2(s));
     }
     byte[] bytes = Encoding.UTF8.GetBytes(s);
     using (var stream = new MemoryStream())
     {
         var len = BitConverter.GetBytes(bytes.Length);
         stream.Write(len, 0, 4);
         using (var compressionStream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Compress))
         {
             compressionStream.Write(bytes, 0, bytes.Length);
         }
         return(Convert.ToBase64String(stream.ToArray()));
     }
 }
コード例 #51
0
        public byte[] ToBytes <T>(T obj, Type[] types = null)
        {
            String json = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.All
            });

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json);

            using (var stream = new MemoryStream())
            {
                using (var gstream = new System.IO.Compression.GZipStream(stream, CompressionMode.Compress))
                {
                    gstream.Write(bytes, 0, bytes.Length);
                }
                return(stream.GetBuffer());
            }
        }
コード例 #52
0
        private void btn_send_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            using (FileStream sw = new FileStream(saveFileDialog1.FileName, FileMode.Create))
            {
                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                CryptoStream encStream = new CryptoStream(sw,
                                                          tdes.CreateEncryptor(exportfilepassword.key,
                                                                               exportfilepassword.vec),
                                                          CryptoStreamMode.Write
                                                          );

                System.IO.Compression.GZipStream gcp = new System.IO.Compression.GZipStream(encStream, CompressionMode.Compress, false);
                byte[] bt_buff = System.Text.Encoding.Default.GetBytes(txt_msg.Text);
                gcp.Write(bt_buff, 0, bt_buff.Length);
                gcp.Close();
            }
            txt_msg.Text = "";
        }
コード例 #53
0
 public static byte[] ZipBytes(this byte[] byteArray)
 {
     try
     {
         //Prepare for compress
         using (var ms = new System.IO.MemoryStream())
         {
             using (var sw = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress))
             {
                 //Compress
                 sw.Write(byteArray, 0, byteArray.Length);
             }
             //Transform byte[] zip data to string
             return(ms.ToArray());
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
コード例 #54
0
        public static ArraySegment <byte> CompressBuffer(ArraySegment <byte> buffer, BufferManager bufferManager, int messageOffset)
        {
            MemoryStream stream = new MemoryStream();

            stream.Write(buffer.Array, 0, messageOffset);

            using (System.IO.Compression.GZipStream gzStrream = new System.IO.Compression.GZipStream(stream, CompressionMode.Compress, true))
            {
                gzStrream.Write(buffer.Array, messageOffset, buffer.Count);
            }

            byte[] compressedBytes = stream.ToArray();
            byte[] bufferedBytes   = bufferManager.TakeBuffer(compressedBytes.Length);

            Array.Copy(compressedBytes, 0, bufferedBytes, 0, compressedBytes.Length);

            bufferManager.ReturnBuffer(buffer.Array);
            ArraySegment <byte> byteArray = new ArraySegment <byte>(bufferedBytes, messageOffset, bufferedBytes.Length - messageOffset);

            return(byteArray);
        }
コード例 #55
0
        /// <summary>
        /// Creates a binary patch (in <a href="http://www.daemonology.net/bsdiff/">bsdiff</a> format) that can be used
        /// (by <see cref="Apply"/>) to transform <paramref name="oldData"/> into <paramref name="newData"/>.
        /// </summary>
        /// <param name="oldData">The original binary data.</param>
        /// <param name="newData">The new binary data.</param>
        /// <param name="output">A <see cref="Stream"/> to which the patch will be written.</param>
        public static void Create(byte[] oldData, byte[] newData, Stream output)
        {
            // check arguments
            if (oldData == null)
            {
                throw new ArgumentNullException("oldData");
            }
            if (newData == null)
            {
                throw new ArgumentNullException("newData");
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (!output.CanSeek)
            {
                throw new ArgumentException("Output stream must be seekable.", "output");
            }
            if (!output.CanWrite)
            {
                throw new ArgumentException("Output stream must be writable.", "output");
            }

            /* Header is
             *      0	8	 "BSDIFF40"
             *      8	8	length of bzip2ed ctrl block
             *      16	8	length of bzip2ed diff block
             *      24	8	length of new file */
            /* File is
             *      0	32	Header
             *      32	??	Bzip2ed ctrl block
             *      ??	??	Bzip2ed diff block
             *      ??	??	Bzip2ed extra block */
            byte[] header = new byte[c_headerSize];
            WriteInt64(c_fileSignature, header, 0);             // "BSDIFF40"
            WriteInt64(0, header, 8);
            WriteInt64(0, header, 16);
            WriteInt64(newData.Length, header, 24);

            long startPosition = output.Position;

            output.Write(header, 0, header.Length);

            int[] I = SuffixSort(oldData);

            byte[] db = new byte[newData.Length];
            byte[] eb = new byte[newData.Length];

            int dblen = 0;
            int eblen = 0;

            using (System.IO.Compression.GZipStream bz2Stream = new System.IO.Compression.GZipStream(output, CompressionLevel.Optimal, true))
            {
                // compute the differences, writing ctrl as we go
                int scan       = 0;
                int pos        = 0;
                int len        = 0;
                int lastscan   = 0;
                int lastpos    = 0;
                int lastoffset = 0;
                while (scan < newData.Length)
                {
                    int oldscore = 0;

                    for (int scsc = scan += len; scan < newData.Length; scan++)
                    {
                        len = Search(I, oldData, newData, scan, 0, oldData.Length, out pos);

                        for (; scsc < scan + len; scsc++)
                        {
                            if ((scsc + lastoffset < oldData.Length) && (oldData[scsc + lastoffset] == newData[scsc]))
                            {
                                oldscore++;
                            }
                        }

                        if ((len == oldscore && len != 0) || (len > oldscore + 8))
                        {
                            break;
                        }

                        if ((scan + lastoffset < oldData.Length) && (oldData[scan + lastoffset] == newData[scan]))
                        {
                            oldscore--;
                        }
                    }

                    if (len != oldscore || scan == newData.Length)
                    {
                        int s    = 0;
                        int sf   = 0;
                        int lenf = 0;
                        for (int i = 0; (lastscan + i < scan) && (lastpos + i < oldData.Length);)
                        {
                            if (oldData[lastpos + i] == newData[lastscan + i])
                            {
                                s++;
                            }
                            i++;
                            if (s * 2 - i > sf * 2 - lenf)
                            {
                                sf   = s;
                                lenf = i;
                            }
                        }

                        int lenb = 0;
                        if (scan < newData.Length)
                        {
                            s = 0;
                            int sb = 0;
                            for (int i = 1; (scan >= lastscan + i) && (pos >= i); i++)
                            {
                                if (oldData[pos - i] == newData[scan - i])
                                {
                                    s++;
                                }
                                if (s * 2 - i > sb * 2 - lenb)
                                {
                                    sb   = s;
                                    lenb = i;
                                }
                            }
                        }

                        if (lastscan + lenf > scan - lenb)
                        {
                            int overlap = (lastscan + lenf) - (scan - lenb);
                            s = 0;
                            int ss   = 0;
                            int lens = 0;
                            for (int i = 0; i < overlap; i++)
                            {
                                if (newData[lastscan + lenf - overlap + i] == oldData[lastpos + lenf - overlap + i])
                                {
                                    s++;
                                }
                                if (newData[scan - lenb + i] == oldData[pos - lenb + i])
                                {
                                    s--;
                                }
                                if (s > ss)
                                {
                                    ss   = s;
                                    lens = i + 1;
                                }
                            }

                            lenf += lens - overlap;
                            lenb -= lens;
                        }

                        for (int i = 0; i < lenf; i++)
                        {
                            db[dblen + i] = (byte)(newData[lastscan + i] - oldData[lastpos + i]);
                        }
                        for (int i = 0; i < (scan - lenb) - (lastscan + lenf); i++)
                        {
                            eb[eblen + i] = newData[lastscan + lenf + i];
                        }

                        dblen += lenf;
                        eblen += (scan - lenb) - (lastscan + lenf);

                        byte[] buf = new byte[8];
                        WriteInt64(lenf, buf, 0);
                        bz2Stream.Write(buf, 0, 8);

                        WriteInt64((scan - lenb) - (lastscan + lenf), buf, 0);
                        bz2Stream.Write(buf, 0, 8);

                        WriteInt64((pos - lenb) - (lastpos + lenf), buf, 0);
                        bz2Stream.Write(buf, 0, 8);

                        lastscan   = scan - lenb;
                        lastpos    = pos - lenb;
                        lastoffset = pos - scan;
                    }
                }
            }

            // compute size of compressed ctrl data
            long controlEndPosition = output.Position;

            WriteInt64(controlEndPosition - startPosition - c_headerSize, header, 8);

            // write compressed diff data
            using (System.IO.Compression.GZipStream bz2Stream = new System.IO.Compression.GZipStream(output, CompressionLevel.Optimal, true))
            {
                bz2Stream.Write(db, 0, dblen);
            }

            // compute size of compressed diff data
            long diffEndPosition = output.Position;

            WriteInt64(diffEndPosition - controlEndPosition, header, 16);

            // write compressed extra data
            using (System.IO.Compression.GZipStream bz2Stream = new System.IO.Compression.GZipStream(output, CompressionLevel.Optimal, true))
            {
                bz2Stream.Write(eb, 0, eblen);
            }

            // seek to the beginning, write the header, then seek back to end
            long endPosition = output.Position;

            output.Position = startPosition;
            output.Write(header, 0, header.Length);
            output.Position = endPosition;
        }
コード例 #56
0
        /// <summary>
        /// Compresses a byte array using a specified algorithm.
        /// </summary>
        /// <param name="dataToCompress">The byte array to compress.</param>
        /// <param name="startIndex">The zero-based position in the array where the compression begins.</param>
        /// <param name="algorithm">The algorithm used to compress the array.
        /// This method will check the compression rate after the compression is done,
        /// and if it is not smaller than 1 (indicating the "compressed" data takes even more space than the original data and the compression is ineffective),
        /// the original data will be returned and this argument will be set <c>ByteCompressionMethods.None</c>.</param>
        /// <returns>
        /// The compressed byte array if the compression is successful;
        /// or the bytes in the original byte array from <paramref name="startIndex"/> to the end if the compression is ineffective.
        /// </returns>
        public static byte[] Compress(this byte[] dataToCompress, int startIndex, ref ByteCompressionMethods algorithm)
        {
            byte[] compressedData;
            using (MemoryStream compressedMs = new MemoryStream())
            {
                switch (algorithm)
                {
                case ByteCompressionMethods.GZipOptimal:
                {
                    using (GZipStream gzs = new GZipStream(compressedMs, CompressionLevel.Optimal))
                        gzs.Write(dataToCompress, startIndex, dataToCompress.Length - startIndex);

                    compressedData = compressedMs.ToArray();

                    break;
                }

                case ByteCompressionMethods.GZipFast:
                {
                    using (GZipStream gzs = new GZipStream(compressedMs, CompressionLevel.Fastest))
                        gzs.Write(dataToCompress, startIndex, dataToCompress.Length - startIndex);

                    compressedData = compressedMs.ToArray();

                    break;
                }

                case ByteCompressionMethods.DeflateOptimal:
                {
                    using (DeflateStream ds = new DeflateStream(compressedMs, CompressionLevel.Optimal))
                        ds.Write(dataToCompress, startIndex, dataToCompress.Length - startIndex);
                    compressedData = compressedMs.ToArray();

                    break;
                }

                case ByteCompressionMethods.DeflateFast:
                {
                    using (DeflateStream ds = new DeflateStream(compressedMs, CompressionLevel.Fastest))
                        ds.Write(dataToCompress, startIndex, dataToCompress.Length - startIndex);
                    compressedData = compressedMs.ToArray();

                    break;
                }

                default:
                {
                    var tmpMs = new MemoryStream(dataToCompress, startIndex, dataToCompress.Length - startIndex);
                    return(tmpMs.ToArray());
                }
                }
            }

            if (compressedData.Length < dataToCompress.Length)
            {
                return(compressedData);
            }
            else
            {
                algorithm = ByteCompressionMethods.None;
                return(dataToCompress);
            }
        }