public static void CreateHugeGzipFile() { if (System.IO.File.Exists(fileName)) { return; } byte[] buffer = new byte[ONE_MEGABYTE]; // 1MB using (System.IO.FileStream gzipTargetAsStream = System.IO.File.OpenWrite(fileName)) { using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream gzipStream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(gzipTargetAsStream)) { try { for (int i = 0; i < REPEAT_COUNT; ++i) { gzipStream.Write(buffer, 0, ONE_MEGABYTE); } // Next i } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } } // End Using gzipStream } // End Using gzipTargetAsStream } // End Sub CreateHugeGzipFile
private void CompresNowGZip() { Stream s = null; Stream aux = new MemoryStream(); try { s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(aux); s.Write(this.m_byteinput, 0, (int)this.m_byteinput.Length); (s as ICSharpCode.SharpZipLib.GZip.GZipOutputStream).Finish(); s.Flush(); } catch (ZipException e) { this.m_byteoutput = null; this.m_streamoutput = null; Debug.WriteLine(e.Message); } finally { aux.Seek(0, SeekOrigin.Begin); this.m_byteoutput = null; this.m_byteoutput = new byte[aux.Length]; aux.Read(this.m_byteoutput, 0, (int)aux.Length); this.m_streamoutput = null; this.m_streamoutput = new MemoryStream(this.m_byteoutput); //s.Close(); } }
public static void WriteToGzip(this IEnumerable <byte> array, string file) { using (var gzip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(File.Create(file + ".gz"))) { gzip.SetLevel(9); var buffer = array.ToArray(); gzip.Write(buffer, 0, buffer.Length); } }
/// <summary> /// Compresses a byte array using the specified compression, and returns a compressed byte array. /// </summary> /// <param name="bytes">The input bytes to compress.</param> /// <param name="offset">The amount of offset to apply to the byte array before beginning compression.</param> /// <param name="length">The length of bytes to compress in the byte array.</param> /// <param name="compressionType">Type of the compression to apply.</param> /// <returns>A byte array representing the compressed byte array</returns> public static byte[] CompressToBytes(byte[] bytes, int offset, int length, CompressionType compressionType) { if (bytes == null || bytes.Length == 0) { return(new byte[0]); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "offset cannot be less than zero"); } if (length > bytes.Length) { throw new ArgumentOutOfRangeException("length", "length cannot be greater than bytes.length"); } if (length + offset > bytes.Length) { throw new ArgumentOutOfRangeException("length", "length + offset cannot be greater than bytes.length"); } using (MemoryStream memoryStream = new MemoryStream()) { switch (compressionType) { case CompressionType.BZip: using (ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(memoryStream)) { stream.Write(bytes, offset, length); } break; case CompressionType.GZip: using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memoryStream)) { stream.Write(bytes, offset, length); } break; // case CompressionType.Tar: // using (ICSharpCode.SharpZipLib.Tar.TarOutputStream stream = new ICSharpCode.SharpZipLib.Tar.TarOutputStream(memoryStream)) { // ICSharpCode.SharpZipLib.Tar.TarEntry entry = ICSharpCode.SharpZipLib.Tar.TarEntry.CreateTarEntry("TarEntry"); // entry.Size = length; // stream.PutNextEntry(entry); // stream.Write(bytes, offset, length); // stream.IsStreamOwner = false; // stream.CloseEntry(); // } // break; case CompressionType.Zip: using (ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(memoryStream)) { stream.Write(bytes, offset, length); } break; default: return(new byte[0]); } return(memoryStream.ToArray()); } }
/// <summary> /// 压缩byte[] /// </summary> /// <returns>The bytes.</returns> /// <param name="data">Data.</param> public static byte[] CompressBytes(byte[] data) { MemoryStream ms = new MemoryStream(); ICSharpCode.SharpZipLib.GZip.GZipOutputStream gzp = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms); gzp.Write(data, 0, data.Length); gzp.Close(); return(ms.ToArray()); }
/// <summary> /// 压缩字节数组 /// 返回:已压缩的字节数组 /// </summary> /// <param name="byteData">待压缩的字节数组</param> /// <returns></returns> public static byte[] CompressBytes(byte[] byteData) { var o = new MemoryStream(); var s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(o); s.Write(byteData, 0, byteData.Length); s.Close(); o.Flush(); o.Close(); return(o.ToArray()); }
public static byte[] GCompress(byte[] data) { byte[] result = null; using (MemoryStream ms = new MemoryStream()) using (Stream s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms)) { s.Write(data, 0, data.Length); s.Close(); result = ms.ToArray(); } return(result); }
private static void CreateZipFile(string lpSourceFile, string lpZipFile, GZipResult result) { byte[] buffer; FileStream fsOut = null; FileStream fsIn = null; ICSharpCode.SharpZipLib.GZip.GZipOutputStream gzip = null; // compress the file into the zip file try { fsOut = new FileStream(lpZipFile, FileMode.Create, FileAccess.Write, FileShare.None); gzip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(fsOut); fsIn = new FileStream(lpSourceFile, FileMode.Open, FileAccess.Read, FileShare.Read); buffer = new byte[fsIn.Length]; fsIn.Read(buffer, 0, buffer.Length); fsIn.Close(); fsIn = null; // compress to the zip file gzip.Write(buffer, 0, buffer.Length); result.ZipFileSize = fsOut.Length; result.CompressionPercent = GetCompressionPercent(result.TempFileSize, result.ZipFileSize); } catch { //(Exception ex1) result.Errors = true; } finally { if (gzip != null) { gzip.Close(); gzip = null; } if (fsOut != null) { fsOut.Close(); fsOut = null; } if (fsIn != null) { fsIn.Close(); fsIn = null; } } }
public void StoreEntries(IEnumerable <string> entries) { using (var compressedStream = bucketStreamProvider.OpenWrite(address)) using (var zip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(compressedStream)) using (var bw = new BinaryWriter(zip)) { zip.SetLevel(9); bw.Write((int)entries.Count()); foreach (var e in entries) { var bytes = Encoding.UTF8.GetBytes(e); bw.Write((int)bytes.Length); zip.Write(bytes, 0, bytes.Length); } } }
public static Byte[] Compress(this byte[] bytes) { if (bytes == null || bytes.Length == 0) { return(new Byte[0]); } using (System.IO.MemoryStream wynik = new System.IO.MemoryStream()) { using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream zip = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(wynik)) { zip.Write(bytes, 0, bytes.Length); zip.Finish(); return(wynik.ToArray()); } } }
/// <summary> /// 压缩字符串 /// </summary> /// <param name="param"></param> /// <returns></returns> public static string Compress(string param) { byte[] data = System.Text.Encoding.UTF8.GetBytes(param); MemoryStream ms = new MemoryStream(); Stream stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms); try { stream.Write(data, 0, data.Length); } finally { stream.Close(); ms.Close(); } return(Convert.ToBase64String(ms.ToArray()).Trim().Replace("+", "@")); }
public string Compress(string inputString) { // Create the zip stream System.IO.MemoryStream memstream = new System.IO.MemoryStream(); ICSharpCode.SharpZipLib.GZip.GZipOutputStream zipstream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memstream); // Get the bytes of the input string Byte[] buffer = System.Text.Encoding.Unicode.GetBytes(inputString); // Write the bytes to the zipstream zipstream.Write(buffer, 0, buffer.Length); // Clean up zipstream.Close(); memstream.Close(); // Get the Base64 representation of the compressed string buffer = memstream.ToArray(); string Result = Convert.ToBase64String(buffer); return(Result); }
public string Compress(string inputString) { // Create the zip stream System.IO.MemoryStream memstream = new System.IO.MemoryStream(); ICSharpCode.SharpZipLib.GZip.GZipOutputStream zipstream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memstream); // Get the bytes of the input string Byte[] buffer = System.Text.Encoding.Unicode.GetBytes(inputString); // Write the bytes to the zipstream zipstream.Write(buffer, 0, buffer.Length); // Clean up zipstream.Close(); memstream.Close(); // Get the Base64 representation of the compressed string buffer = memstream.ToArray(); string Result = Convert.ToBase64String(buffer); return Result; }
/// <summary> /// Compresses a byte array using the specified compression, and returns a compressed byte array. /// </summary> /// <param name="bytes">The input bytes to compress.</param> /// <param name="offset">The amount of offset to apply to the byte array before beginning compression.</param> /// <param name="length">The length of bytes to compress in the byte array.</param> /// <param name="compressionType">Type of the compression to apply.</param> /// <returns>A byte array representing the compressed byte array</returns> public static byte[] CompressToBytes(byte[] bytes, int offset, int length, CompressionType compressionType) { if (bytes == null || bytes.Length == 0) return new byte[0]; if (offset < 0) throw new ArgumentOutOfRangeException("offset", "offset cannot be less than zero"); if (length > bytes.Length) throw new ArgumentOutOfRangeException("length", "length cannot be greater than bytes.length"); if (length + offset > bytes.Length) throw new ArgumentOutOfRangeException("length", "length + offset cannot be greater than bytes.length"); using (MemoryStream memoryStream = new MemoryStream()) { switch (compressionType) { case CompressionType.BZip: using (ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(memoryStream)) { stream.Write(bytes, offset, length); } break; case CompressionType.GZip: using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memoryStream)) { stream.Write(bytes, offset, length); } break; // case CompressionType.Tar: // using (ICSharpCode.SharpZipLib.Tar.TarOutputStream stream = new ICSharpCode.SharpZipLib.Tar.TarOutputStream(memoryStream)) { // ICSharpCode.SharpZipLib.Tar.TarEntry entry = ICSharpCode.SharpZipLib.Tar.TarEntry.CreateTarEntry("TarEntry"); // entry.Size = length; // stream.PutNextEntry(entry); // stream.Write(bytes, offset, length); // stream.IsStreamOwner = false; // stream.CloseEntry(); // } // break; case CompressionType.Zip: using (ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(memoryStream)) { stream.Write(bytes, offset, length); } break; default: return new byte[0]; } return memoryStream.ToArray(); } }
private int RecursivelyPersistLayer(XbimSqliteDB db, XbimMeshLayer<XbimMeshGeometry3D, XbimRenderMaterial> layer, int layerid, int parentLayerId) { try { // bytes contain 6 floats and 1 int // pointX, pointY, pointZ, normalX, normalY, normalZ, entityLabel // this is the vbo. XbimTexture texture = layer.Style; XbimColour c = layer.Style.ColourMap[0]; //take the first one, not perhaps correct or best practice byte[] colour = new byte[4 * sizeof(float)]; MemoryStream ms = new MemoryStream(colour); BinaryWriter bw = new BinaryWriter(ms); bw.Write(c.Red); bw.Write(c.Green); bw.Write(c.Blue); bw.Write(c.Alpha); byte[] vbo = ((XbimMeshGeometry3D)layer.Hidden).ToByteArray(); MemoryStream memoryStream = new MemoryStream(); using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream zis = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(memoryStream)) { zis.Write(vbo, 0, vbo.Length); memoryStream.Flush(); } vbo = memoryStream.ToArray(); // bounding box XbimRect3D bb = layer.BoundingBoxHidden(); byte[] bbArray = bb.ToFloatArray(); db.AddLayer(layer.Name, layerid, parentLayerId, colour, vbo, bbArray); int myParent = layerid; foreach (var subLayer in layer.SubLayers) { layerid++; layerid = RecursivelyPersistLayer(db, subLayer, layerid, myParent); } return layerid; } catch (Exception ex) { throw new XbimException("Error building scene layer", ex); } }