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
public void ProcessAction(Object eventObject) { lock (blacklistLock) { if(blacklist.Contains(eventObject)) { blacklist.Remove(eventObject); return; } } var memoryStream = new MemoryStream(); var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress); var writer = new StreamWriter(gzipStream); serializer.Serialize(writer, eventObject); writer.Flush(); gzipStream.Flush(); memoryStream.Flush(); writer.Close(); gzipStream.Close(); memoryStream.Close(); var obj = new EventObject { MessageType = eventObject.GetType().AssemblyQualifiedName, MessageBody = memoryStream.ToArray(), SessionId = sessionId }; eventRepository.Save(obj); }
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; }
public ManagedBitmap(string fileName) { try { byte[] buffer = new byte[1024]; MemoryStream fd = new MemoryStream(); Stream fs = File.OpenRead(fileName); using (Stream csStream = new GZipStream(fs, CompressionMode.Decompress)) { int nRead; while ((nRead = csStream.Read(buffer, 0, buffer.Length)) > 0) { fd.Write(buffer, 0, nRead); } csStream.Flush(); buffer = fd.ToArray(); } Width = buffer[4] << 8 | buffer[5]; Height = buffer[6] << 8 | buffer[7]; _colors = new Color[Width * Height]; int start = 8; for (int i = 0; i < _colors.Length; i++) { _colors[i] = Color.FromArgb(buffer[start], buffer[start + 1], buffer[start + 2], buffer[start + 3]); start += 4; } } catch { LoadedOk = false; } }
public static void WriteGzip(XmlDocument theDoc, Stream theStream) { var ms = new MemoryStream(); var xmlSettings = new XmlWriterSettings { Encoding = Encoding.UTF8, ConformanceLevel = ConformanceLevel.Document, Indent = false, NewLineOnAttributes = false, CheckCharacters = true, IndentChars = string.Empty }; XmlWriter tw = XmlWriter.Create(ms, xmlSettings); theDoc.WriteTo(tw); tw.Flush(); tw.Close(); byte[] buffer = ms.GetBuffer(); var 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(); }
private static int Gzip(Stream stream, FileStream fs) { int length = 0; using (var ms = new MemoryStream()) { byte[] buffer = new byte[BufferSize]; int count; using (var gzip = new GZipStream(ms, CompressionMode.Compress, true)) { while ((count = fs.Read(buffer, 0, BufferSize)) > 0) { gzip.Write(buffer, 0, count); } gzip.Flush(); } ms.Seek(0, SeekOrigin.Begin); while ((count = ms.Read(buffer, 0, BufferSize)) > 0) { length += count; stream.Write(buffer, 0, count); } stream.Flush(); } return length; }
private MemoryStream Compression(byte[] bytes) { MemoryStream ms = new MemoryStream(); GZipStream gzip = new GZipStream(ms, CompressionLevel.Optimal); gzip.Write(bytes, 0, bytes.Length); gzip.Flush(); gzip.Close(); return ms; }
public static byte[] Zip(byte[] bytes) { using (var destination = new MemoryStream()) using (var gzip = new GZipStream(destination, CompressionLevel.Fastest, false)) { gzip.Write(bytes, 0, bytes.Length); gzip.Flush(); gzip.Close(); return destination.ToArray(); } }
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()); }
public static byte[] GZipCompress(byte[] source) { var memoryStream = new MemoryStream(); var stream = new GZipStream(memoryStream, CompressionMode.Compress); stream.Write(source, 0, source.Length); stream.Flush(); stream.Close(); //Console.WriteLine("GZip : {0}", memoryStream.ToArray().Length); return memoryStream.ToArray(); }
public static void Save(ArpeggioDefinition arpeggio, string fileName) { Stream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); GZipStream gzip = new GZipStream(fileStream, CompressionMode.Compress, true); SerializationUtils.Serialize(arpeggio, gzip); gzip.Flush(); gzip.Close(); fileStream.Close(); fileStream.Dispose(); }
public static byte[] Unzip(byte[] bytes) { using (var source = new MemoryStream(bytes)) using (var destination = new MemoryStream()) using (var gzip = new GZipStream(source, CompressionMode.Decompress, false)) { gzip.CopyTo(destination); gzip.Flush(); gzip.Close(); return destination.ToArray(); } }
public static byte[] Compress(this byte[] data) { using (MemoryStream ms = new MemoryStream()) { using (GZipStream zs = new GZipStream(ms, CompressionMode.Compress)) { zs.Write(data, 0, data.Length); zs.Flush(); } return ms.ToArray(); } }
/// <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()); } }
/// <summary> /// 解压二进制流 /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] Depress(byte[] data) { using (var memoryStream = new MemoryStream(data)) using (var outStream = new MemoryStream()) { using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Decompress)) { compressionStream.CopyTo(outStream); compressionStream.Flush(); } return outStream.ToArray(); } }
/// <summary> /// 压缩二进制流 /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] Compress(byte[] data) { using (var memoryStream = new MemoryStream()) { using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Compress)) { compressionStream.Write(data, 0, data.Length); compressionStream.Flush(); } //必须先关了compressionStream后才能取得正确的压缩流 return memoryStream.ToArray(); } }
public Stream Compress(string content) { var ms = new MemoryStream(); using (var zip = new GZipStream(ms, CompressionMode.Compress, true)) { var buffer = Encoding.UTF8.GetBytes(content); zip.Write(buffer, 0, buffer.Length); zip.Flush(); } ms.Position = 0; return ms; }
public static void WriteDataToRequest(HttpWebRequest req, string data) { req.SendChunked = true; using (var requestStream = req.GetRequestStream()) using (var dataStream = new GZipStream(requestStream, CompressionMode.Compress)) using (var writer = new StreamWriter(dataStream, Encoding.UTF8)) { writer.Write(data); writer.Flush(); dataStream.Flush(); requestStream.Flush(); } }
public StoredScript(String data, LighteningDataType dataType) { DataType = dataType; Uncompressed = Encoding.UTF8.GetBytes(data); using (MemoryStream result = new MemoryStream()) { using (GZipStream gzStream = new GZipStream(result, CompressionLevel.Optimal, true)) { gzStream.Write(Uncompressed, 0, Uncompressed.Length); gzStream.Flush(); } Compressed = result.ToArray(); } }
/// <summary> /// Compress the content of a stream. The /// </summary> /// <param name="data">Data to compress</param> /// <param name="compressedData">Stream with the compressed data</param> public static void Compress(ref Stream data, out Stream compressedData) { compressedData = new MemoryStream(); var compress = new GZipStream(compressedData, CompressionMode.Compress); data.Position = 0; var buffer = new byte[data.Length]; int n; while ((n = data.Read(buffer, 0, buffer.Length)) != 0) compress.Write(buffer, 0, n); compress.Flush(); data.Close(); }
public static void FolderToFile(string srcPath, string dstFilePath) { // list out name+hash -> [path] // write this to a single file // gzip that file var tmp = dstFilePath+".tmp"; var bits = new Dictionary<string, List<string>>(); // find distinct files var files = NativeIO.EnumerateFiles(new PathInfo(srcPath).FullNameUnc, searchOption: SearchOption.AllDirectories); foreach (var file in files) { var name = file.Name; var hash = HashOf(file); Add(name + "|" + hash, file.FullName, bits); } // pack everything into a temp file if (File.Exists(tmp)) File.Delete(tmp); using(var fs = File.OpenWrite(tmp)) foreach (var key in bits.Keys) { var paths = bits[key]; var catPaths = Encoding.UTF8.GetBytes(string.Join("|", Filter(paths,srcPath))); WriteLength(catPaths.Length, fs); fs.Write(catPaths, 0, catPaths.Length); var info = NativeIO.ReadFileDetails(new PathInfo(paths[0])); WriteLength((long)info.Length, fs); using (var inf = NativeIO.OpenFileStream(info.PathInfo, FileAccess.Read)) inf.CopyTo(fs); fs.Flush(); } // Compress the file if (File.Exists(dstFilePath)) File.Delete(dstFilePath); using (var compressing = new GZipStream(File.OpenWrite(dstFilePath), CompressionLevel.Optimal)) using (var cat = File.OpenRead(tmp)) { cat.CopyTo(compressing, 65536); compressing.Flush(); } // Kill the temp file File.Delete(tmp); }
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(); }
/// <summary> /// 压缩 /// </summary> /// <param name="value"></param> /// <returns></returns> public static string CompressString(string value) { byte[] byteArray = Encoding.UTF8.GetBytes(value); byte[] tmpArray; using (MemoryStream ms = new MemoryStream()) { using (GZipStream sw = new GZipStream(ms, CompressionMode.Compress)) { sw.Write(byteArray, 0, byteArray.Length); sw.Flush(); } tmpArray = ms.ToArray(); } return Convert.ToBase64String(tmpArray); }
/* public Blib(string FileName) { if (System.IO.File.Exists(FileName)) Load(FileName); else { _files = new List<File>(); Save(FileName); } } */ public void Save(string fileName) { FileStream f = System.IO.File.OpenWrite(fileName); var gz = new GZipStream(f, CompressionLevel.Optimal); gz.Write(BitConverter.GetBytes(_files.Count), 0, 4); foreach (File file in _files) { byte[] binName = Encoding.Default.GetBytes(file.FileName); gz.Write(BitConverter.GetBytes(binName.Length), 0, 4); gz.Write(binName, 0, binName.Length); gz.Write(BitConverter.GetBytes(file.Content.Length), 0, 4); gz.Write(file.Content, 0, file.Content.Length); } gz.Flush(); gz.Close(); f.Close(); }
public static void WriteDataToRequest(HttpWebRequest req, string data, bool disableCompression) { req.SendChunked = true; // we want to make sure that we use a buffer properly here so we won't send the data // in many different TCP packets using (var requestStream = new BufferedStream(req.GetRequestStream())) using (var dataStream = new GZipStream(requestStream, CompressionMode.Compress)) using (var writer = disableCompression == false ? new StreamWriter(dataStream, Encoding.UTF8) : new StreamWriter(requestStream, Encoding.UTF8)) { writer.Write(data); writer.Flush(); if (disableCompression == false) dataStream.Flush(); requestStream.Flush(); } }
public static string Compress(string data) { MemoryStream decompressMS = new MemoryStream(); GZipStream zip = new GZipStream(decompressMS, CompressionMode.Compress); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data); zip.Write(buffer, 0, buffer.Length); zip.Flush(); zip.Close(); byte[] temp = decompressMS.ToArray(); string result = Convert.ToBase64String(temp); decompressMS.Close(); return result; }
/* INPUT 1: ../../Milioni.mp4 ../../ 5 INPUT 2: ../../text.txt ../../ 3 * * * */ private static void Slice(string sourceFile, string destinationDirectory, int parts) { FileStream reader = new FileStream(sourceFile, FileMode.Open); FileInfo file = new FileInfo(sourceFile); long chunkSize = (long)(file.Length/parts); BigInteger counter = -1; if (file.Length%2 == 1) { counter = 0; } int fileCounter = 1; int readBytesVariable = reader.ReadByte(); List<byte> lsBytes = new List<byte>(); lsBytes.Add((byte)readBytesVariable); while (readBytesVariable != -1) { if ((counter%chunkSize == 0 && counter != 0) || counter == file.Length) { string fileName = destinationDirectory + "Part-" + fileCounter + ".gz"; FileStream writer = new FileStream(fileName, FileMode.Create,FileAccess.Write); GZipStream gzip = new GZipStream(writer, CompressionMode.Compress); gzip.Write(lsBytes.ToArray(), 0,lsBytes.Count); gzip.Flush(); gzip.Dispose(); writer.Dispose(); lsBytes.Clear(); fileCounter++; } readBytesVariable = reader.ReadByte(); lsBytes.Add((byte)readBytesVariable); counter++; } }
/// <summary> /// ��ѹ������ ���ַ��� /// </summary> /// <param name="compressedString">�ַ���</param> public string dbsDeCompress(byte[] byteInput) { try { //string uncompressedString=string.Empty; StringBuilder sb = new StringBuilder(409600); int totalLength = 0; // byte[] byteInput = System.Convert.FromBase64String(compressedString); byte[] writeData = new byte[409600]; //Stream s = new GZipInputStream(new MemoryStream(byteInput)); //decompressedStream=newGZipStream(sourceStream,CompressionMode.Decompress,true); Stream s = new GZipStream(new MemoryStream(byteInput), CompressionMode.Decompress, true); while (true) { int size = s.Read(writeData, 0, writeData.Length); if (size > 0) { totalLength += size; sb.Append(System.Text.Encoding.UTF8.GetString(writeData, 0, size)); } else { break; } } s.Flush(); s.Close(); return sb.ToString(); } catch { int u = 0; return ""; } }
private void WriteToBuffer(List<RavenJObject> localBatch) { using (var gzip = new GZipStream(bufferedStream, CompressionMode.Compress, leaveOpen: true)) { var binaryWriter = new BinaryWriter(gzip); binaryWriter.Write(localBatch.Count); var bsonWriter = new BsonWriter(binaryWriter); foreach (RavenJObject doc in localBatch) { doc.WriteTo(bsonWriter); } bsonWriter.Flush(); binaryWriter.Flush(); gzip.Flush(); } }
/// <summary> /// File-to-file buffered Compression /// </summary> /// <param name="sFileIn"></param> /// <param name="sFileOut"></param> /// <returns></returns> public static bool CompressFile(String sFileIn, String sFileOut) { const int size = 8192; byte[] buffer = new byte[size]; try { using (FileStream fDecom = new FileStream(sFileIn, FileMode.Open, FileAccess.Read)) using (FileStream fCompr = new FileStream(sFileOut, FileMode.Create, FileAccess.Write)) using (GZipStream alg = new GZipStream(fCompr, CompressionMode.Compress)) { int bytesRead = 0; do { // Read buffer bytesRead = fDecom.Read(buffer, 0, buffer.Length); // Write buffer away if (bytesRead > 0) alg.Write(buffer, 0, bytesRead); } while (bytesRead > 0); // finish writing alg.Flush(); alg.Close(); alg.Dispose(); // Finish reading fCompr.Close(); fDecom.Close(); fDecom.Dispose(); } // Return success return true; } catch (Exception ex) { ErrHandle.HandleErr("CompressFile", ex); // Return failure return false; } }
private string httpRequest(String url, String POST = null) { WebRequest httpWebRequest = WebRequest.Create(url); if (POST != null) { byte[] bytes = System.Text.Encoding.ASCII.GetBytes(POST); httpWebRequest.Method = "POST"; using (Stream request = httpWebRequest.GetRequestStream()) { using (var zipStream = new GZipStream(request, CompressionMode.Compress)) { zipStream.Write(bytes, 0, bytes.Length); zipStream.Flush(); zipStream.Close(); } request.Flush(); request.Close(); } } using (StreamReader streamReader = new StreamReader(httpWebRequest.GetResponse().GetResponseStream())) { return streamReader.ReadToEnd(); } }
static long CompressStream(MemoryStream orgStream, string name) { orgStream.Seek(0, SeekOrigin.Begin); var compressedData = new MemoryStream(); var gzip = new GZipStream(compressedData, CompressionMode.Compress); orgStream.CopyTo(gzip); gzip.Flush(); orgStream.Seek(0, SeekOrigin.Begin); using (var fs = new FileStream(Path.Combine(@"C:\temp\comptest\", name + ".bin"), FileMode.Create, FileAccess.Write)) { orgStream.CopyTo(fs); } return compressedData.Length; }
public void Save(string path) { Stream.SetLength(0); SaveTag(File); using (GZipStream gStream = new GZipStream(new FileStream(path, FileMode.Create), CompressionMode.Compress)) { Stream.CopyTo(gStream); gStream.Flush(); } }
public byte[] Serialize(Character character) { if (character == null) return null; try { using (MemoryStream stream = new MemoryStream()) using (GZipStream gzip = new GZipStream(stream, CompressionMode.Compress)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(gzip, character); gzip.Flush(); gzip.Close(); return stream.GetBuffer(); } } catch (SerializationException ex) { Console.WriteLine(ex); return null; } }