private static void CompressFile(string md5) { try { byte [] buffer = new byte [1024]; int read; using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream iso_in = new IsolatedStorageFileStream(md5, FileMode.Open, FileAccess.Read, FileShare.Read, iso)) { using (IsolatedStorageFileStream iso_out = iso.CreateFile(md5 + ".bz")) { using (ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream zip_out = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(iso_out)) { while ((read = iso_in.Read(buffer, 0, buffer.Length)) > 0) { zip_out.Write(buffer, 0, read); } zip_out.Flush(); } } } } IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(md5); IsolatedStorageFile.GetUserStoreForApplication().MoveFile(md5 + ".bz", md5); MainPage.Log("Compressed {0}", md5); } catch (Exception ex) { MainPage.Log("Exception while compressing {0}: {1}", md5, ex); } }
public void SaveToCompressedStream(System.IO.Stream aData) { using (var gzipOut = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData)) { gzipOut.IsStreamOwner = false; SaveToStream(gzipOut); gzipOut.Close(); } }
/// <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> /// /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] CompressBytes(byte[] data) { var o = new MemoryStream(); var s = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(o); s.Write(data, 0, data.Length); s.Close(); o.Flush(); o.Close(); return(o.ToArray()); }
public string Zip(string uncompressedString) { byte[] bytData = System.Text.Encoding.Unicode.GetBytes(uncompressedString); MemoryStream ms = new MemoryStream(); Stream s = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms); s.Write(bytData, 0, bytData.Length); s.Close(); byte[] compressedData = (byte[])ms.ToArray(); return(System.Convert.ToBase64String(compressedData, 0, compressedData.Length)); }
private static void SerializeTo(VocalIndexObject Object, string FilePath) { FileStream msObj = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream boz = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(msObj); DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(VocalIndexObject)); js.WriteObject(boz, Object); boz.Flush(); boz.Close(); msObj.Close(); }
/// <summary> /// Compress <paramref name="instream">input stream</paramref> sending /// result to <paramref name="outputstream">output stream</paramref> /// </summary> /// <param name="outstream"></param> /// <param name="instream"></param> /// <param name="strength">Strngth of the Compression</param> /// <remarks>I had to change the Origial Compression Methode in a way that it woun't close /// the <paramref name="instream">input stream</paramref> and that it seeks to the Beginning /// of it on startup</remarks> protected static void Compress(Stream instream, Stream outstream, CompressionStrength strength) { System.IO.Stream bos = outstream; System.IO.Stream bis = instream; bis.Seek(0, System.IO.SeekOrigin.Begin); int ch = bis.ReadByte(); ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream bzos = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(bos, (int)strength); while (ch != -1) { bzos.WriteByte((byte)ch); ch = bis.ReadByte(); } bzos.Close(); }
public void SerializeToFile(T Object, string FileName, SerializeType Type, bool isZip) { FileStream msObj = new FileStream(FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); BasicFileInformation bfi = GetBasicFileInformation(Object); string pwd = bfi.SavePassword; WriteBasicInformation(Object, msObj); Stream TargetStream; if (pwd.Length > 0) { CryptoStream cs = DesCooler.CreateEncryptStream(pwd, msObj); if (isZip) { TargetStream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(cs); } else { TargetStream = cs; } } else { if (isZip) { TargetStream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(msObj); } else { TargetStream = msObj; } } try { switch (Type) { case SerializeType.JSON: Serialize_JSON(Object, TargetStream); break; case SerializeType.Binary: Serialize_Binary(Object, TargetStream); break; } } catch {; } TargetStream.Flush(); TargetStream.Close(); msObj.Close(); }
/// <summary> /// 压缩 /// </summary> /// <param name="param"></param> /// <returns></returns> public static string Compress(string param) { byte[] data = System.Text.Encoding.UTF8.GetBytes(param); //byte[] data = Convert.FromBase64String(param); MemoryStream ms = new MemoryStream(); Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms); try { stream.Write(data, 0, data.Length); } finally { stream.Close(); ms.Close(); } return(Convert.ToBase64String(ms.ToArray())); }
private void Save(string indexFileName) { using (var file = File.Create(indexFileName)) using (var compressor = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(file)) { var serializer = new NetSerializer.Serializer(new Type[] { typeof(Indexes) }); var indexes = new Indexes(); indexes.ChunkIdToFirstNonFreeObjectInChunk = _chunkIdToFirstNonFreeObjectInChunk; indexes.StartOfChunkToChunkId = _startOfChunkToChunkId; indexes.ChunkToReferencingChunks = _chunkToReferencingChunks; indexes.DirectlyRooted = _directlyRooted.ToArray(); indexes.AllRoots = _allRoots; indexes.StaticRootsEnumerated = _staticRootsEnumerated; indexes.ChunkSize = _chunkSize; serializer.Serialize(compressor, indexes); } _context.WriteLine("Wrote index file of size {0}", ((ulong)new FileInfo(indexFileName).Length).ToMemoryUnits()); }
/// <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 static void CompressFile(string md5) { try { byte [] buffer = new byte [1024]; int read; using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication ()) { using (IsolatedStorageFileStream iso_in = new IsolatedStorageFileStream (md5, FileMode.Open, FileAccess.Read, FileShare.Read, iso)) { using (IsolatedStorageFileStream iso_out = iso.CreateFile (md5 + ".bz")) { using (ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream zip_out = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream (iso_out)) { while ((read = iso_in.Read (buffer, 0, buffer.Length)) > 0) { zip_out.Write (buffer, 0, read); } zip_out.Flush (); } } } } IsolatedStorageFile.GetUserStoreForApplication ().DeleteFile (md5); IsolatedStorageFile.GetUserStoreForApplication ().MoveFile (md5 + ".bz", md5); MainPage.Log ("Compressed {0}", md5); } catch (Exception ex) { MainPage.Log ("Exception while compressing {0}: {1}", md5, ex); } }