public virtual byte[] Compress(byte[] decompressedData) { using (MemoryStream ms = new MemoryStream()) { if (this.Algorithm == CompressionAlgorithm.GZip) { GZipStream stream2 = new GZipStream(ms, CompressionMode.Compress, true); stream2.Write(decompressedData, 0, decompressedData.Length); stream2.Close(); } else if (this.Algorithm == CompressionAlgorithm.Deflate) { DeflateStream stream3 = new DeflateStream(ms, CompressionMode.Compress, true); stream3.Write(decompressedData, 0, decompressedData.Length); stream3.Close(); } else { var bzipStream = new BZip2OutputStream(ms); bzipStream.Write(decompressedData, 0, decompressedData.Length); bzipStream.Close(); } return(ms.ToArray()); } }
public void BasicRoundTrip() { var ms = new MemoryStream(); var outStream = new BZip2OutputStream(ms); byte[] buf = new byte[10000]; var rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); outStream.Close(); ms = new MemoryStream(/*ms.GetBuffer()*/ ms.ToArray()); ms.Seek(0, SeekOrigin.Begin); using (BZip2InputStream inStream = new BZip2InputStream(ms)) { byte[] buf2 = new byte[buf.Length]; // How can the Inflater known the length of original data? int pos = 0; while (true) { int numRead = inStream.Read(buf2, pos, 4096); if (numRead <= 0) { break; } pos += numRead; } for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } } }
public void CreateEmptyArchive() { var ms = new MemoryStream(); var outStream = new BZip2OutputStream(ms); #if NET451 outStream.Close(); #elif NETCOREAPP1_0 outStream.Dispose(); #endif ms = new MemoryStream(ms.ToArray()); ms.Seek(0, SeekOrigin.Begin); using (BZip2InputStream inStream = new BZip2InputStream(ms)) { byte[] buffer = new byte[1024]; int pos = 0; while (true) { int numRead = inStream.Read(buffer, 0, buffer.Length); if (numRead <= 0) { break; } pos += numRead; } Assert.AreEqual(pos, 0); } }
public static string CatGame1(string str, bool isPress = false) { byte[] buffer4; byte[] bytes = Encoding.UTF8.GetBytes(str); byte[] rgbKey = Encoding.UTF8.GetBytes("b5nHjsMrqaeNliSs3jyOzgpD"); byte[] rgbIV = Encoding.UTF8.GetBytes("wuD6keVr"); TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider(); using (MemoryStream stream = new MemoryStream()) { using (CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) { if (isPress) { using (BZip2OutputStream stream3 = new BZip2OutputStream(stream2)) { stream3.Write(bytes, 0, bytes.Length); stream3.Close(); } } else { stream2.Write(bytes, 0, bytes.Length); } stream2.Close(); } buffer4 = stream.ToArray(); stream.Close(); } return(Convert.ToBase64String(buffer4)); }
public static byte[] ZipString(string sBuffer) { MemoryStream m_msBZip2 = null; BZip2OutputStream m_osBZip2 = null; byte[] result; try { m_msBZip2 = new MemoryStream(); Int32 size = sBuffer.Length; // Prepend the compressed data with the length of the uncompressed data (firs 4 bytes) // using (BinaryWriter writer = new BinaryWriter(m_msBZip2, System.Text.Encoding.ASCII)) { writer.Write(size); m_osBZip2 = new BZip2OutputStream(m_msBZip2); m_osBZip2.Write(Encoding.ASCII.GetBytes(sBuffer), 0, sBuffer.Length); m_osBZip2.Close(); result = m_msBZip2.ToArray(); m_msBZip2.Close(); writer.Close(); } } finally { if (m_osBZip2 != null) { m_osBZip2.Dispose(); } if (m_msBZip2 != null) { m_msBZip2.Dispose(); } } return(result); }
public void BasicRoundTrip() { var ms = new MemoryStream(); var outStream = new BZip2OutputStream(ms); var buf = Utils.GetDummyBytes(size: 10000, RandomSeed); outStream.Write(buf, offset: 0, buf.Length); outStream.Close(); ms = new MemoryStream(ms.GetBuffer()); ms.Seek(offset: 0, SeekOrigin.Begin); using BZip2InputStream inStream = new BZip2InputStream(ms); var buf2 = new byte[buf.Length]; var pos = 0; while (true) { var numRead = inStream.Read(buf2, pos, count: 4096); if (numRead <= 0) { break; } pos += numRead; } for (var i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } }
/// <summary> /// Compress the specified sBuffer. /// </summary> /// <param name="sBuffer">S buffer.</param> public static string Compress(string sBuffer) { byte[] compressed = null; string compressedText = null; try { byte[] bytesBuffer = Encoding.UTF8.GetBytes(sBuffer); using (MemoryStream compressStream = new MemoryStream()) { using (BZip2OutputStream bzipStream = new BZip2OutputStream(compressStream)) { bzipStream.Write(bytesBuffer, 0, bytesBuffer.Length); bzipStream.Flush(); bzipStream.Close(); compressed = compressStream.ToArray(); } } } catch (Exception ex) { ConsoleEx.DebugLog("BZip2 compress error : " + ex.ToString()); } finally { if (compressed != null) { compressedText = Convert.ToBase64String(compressed); } } return(compressedText); }
/// <summary> /// 지정된 데이타를 압축한다. /// </summary> /// <param name="input">압축할 Data</param> /// <returns>압축된 Data</returns> public override byte[] Compress(byte[] input) { if (IsDebugEnabled) { log.Debug(CompressorTool.SR.CompressStartMsg); } // check input data if (input.IsZeroLength()) { if (IsDebugEnabled) { log.Debug(CompressorTool.SR.InvalidInputDataMsg); } return(CompressorTool.EmptyBytes); } byte[] output; // Compress using (var outStream = new MemoryStream(input.Length)) using (var bz2 = new BZip2OutputStream(outStream, CompressorTool.BUFFER_SIZE)) { bz2.Write(input, 0, input.Length); bz2.Close(); output = outStream.ToArray(); } if (IsDebugEnabled) { log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length); } return(output); }
private void submitBtn_Click(object sender, EventArgs e) { XmlDocument doc = convForm.ExportToXml(); MemoryStream memStream = new MemoryStream(); BZip2OutputStream bzStream = new BZip2OutputStream(memStream); doc.Save(bzStream); bzStream.Close(); memStream.Close(); try { oSpyRepository.RepositoryService svc = new oSpyClassic.oSpyRepository.RepositoryService(); string permalink = svc.SubmitTrace(nameTextBox.Text, descTextBox.Text, memStream.ToArray()); ShareSuccessForm frm = new ShareSuccessForm(permalink); frm.ShowDialog(this); Close(); } catch (Exception ex) { MessageBox.Show(String.Format("Failed to submit visualization: {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void BasicRoundTrip() { MemoryStream ms = new MemoryStream(); BZip2OutputStream outStream = new BZip2OutputStream(ms); byte[] buf = new byte[10000]; System.Random rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); outStream.Close(); ms = new MemoryStream(ms.GetBuffer()); ms.Seek(0, SeekOrigin.Begin); using (BZip2InputStream inStream = new BZip2InputStream(ms)) { byte[] buf2 = new byte[buf.Length]; int pos = 0; while (true) { int numRead = inStream.Read(buf2, pos, 4096); if (numRead <= 0) { break; } pos += numRead; } for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } } }
public void CreateEmptyArchive() { MemoryStream ms = new MemoryStream(); BZip2OutputStream outStream = new BZip2OutputStream(ms); outStream.Close(); ms = new MemoryStream(ms.GetBuffer()); ms.Seek(0, SeekOrigin.Begin); using (BZip2InputStream inStream = new BZip2InputStream(ms)) { byte[] buffer = new byte[1024]; int pos = 0; while (true) { int numRead = inStream.Read(buffer, 0, buffer.Length); if (numRead <= 0) { break; } pos += numRead; } Assert.AreEqual(pos, 0); } }
// Token: 0x06003B90 RID: 15248 RVA: 0x00138434 File Offset: 0x00136634 public static string CatGame1(string str, bool isCompress = false) { byte[] bytes = Encoding.UTF8.GetBytes(str); byte[] bytes2 = Encoding.UTF8.GetBytes("b************c**********"); byte[] bytes3 = Encoding.UTF8.GetBytes("a****a**"); TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider(); byte[] inArray; using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, tripleDESCryptoServiceProvider.CreateEncryptor(bytes2, bytes3), CryptoStreamMode.Write)) { if (isCompress) { using (BZip2OutputStream bzip2OutputStream = new BZip2OutputStream(cryptoStream)) { bzip2OutputStream.Write(bytes, 0, bytes.Length); bzip2OutputStream.Close(); } } else { cryptoStream.Write(bytes, 0, bytes.Length); } cryptoStream.Close(); } inArray = memoryStream.ToArray(); memoryStream.Close(); } return(Convert.ToBase64String(inArray)); }
override public void AddToStream(Stream stream, EventTracker tracker) { if (ChildCount > 1) { throw new Exception("Bzip2 file " + Uri + " has " + ChildCount + " children"); } if (tracker != null) { tracker.ExpectingAdded(UriFu.UriToEscapedString(this.Uri)); } UnclosableStream unclosable; unclosable = new UnclosableStream(stream); BZip2OutputStream bz2_out; bz2_out = new BZip2OutputStream(unclosable); MemoryStream memory; memory = new MemoryStream(); // There should just be one child foreach (FileObject file in Children) { file.AddToStream(memory, tracker); } bz2_out.Write(memory.ToArray(), 0, (int)memory.Length); memory.Close(); bz2_out.Close(); }
/// <summary> /// Saves a woman to the given path. /// </summary> /// <param name="w">Woman to save.</param> /// <param name="path"><Path to the new/overwritten file./param> /// <returns>True if file successfully saved.</returns> public static bool SaveWomanTo(Woman w, string path) { try { // Serialize to memory stream to make sure data is serializable. var saveMemoryStream = new MemoryStream(); var testSaveDataStream = new BZip2OutputStream(saveMemoryStream, 9); // Make sure serialization goes well. new XmlSerializer(w.GetType()).Serialize(testSaveDataStream, w); testSaveDataStream.Close(); var data = saveMemoryStream.ToArray(); testSaveDataStream.Close(); // Dispose resources. saveMemoryStream.Dispose(); testSaveDataStream.Dispose(); // Prepare to read the saved data. var loadMemoryStream = new MemoryStream(data); var testLoadDataStream = new BZip2InputStream(loadMemoryStream); // Make sure we can read it back. new XmlSerializer(typeof(Woman)).Deserialize(testLoadDataStream); testLoadDataStream.Close(); // Dispose resources. loadMemoryStream.Dispose(); testLoadDataStream.Dispose(); // Make sure we have access to the file system. var filestream = new FileStream(path, FileMode.Create); // Save to file. filestream.Write(data, 0, data.Length); filestream.Close(); } catch (Exception ex) { MsgBox.Error(TEXT.Get["Unable_to_save_file"] + ex.Message, TEXT.Get["Error"]); return(false); } return(true); }
void Writer() { WriteTargetBytes(); #if NET451 outStream_.Close(); #elif NETCOREAPP1_0 outStream_.Dispose(); #endif }
/// <summary> /// BZip2压缩 /// </summary> /// <param name="rawData"></param> /// <returns></returns> public static byte[] BZip2Compress(byte[] rawData) { MemoryStream ms = new MemoryStream(); BZip2OutputStream BZip2 = new BZip2OutputStream(ms); BZip2.Write(rawData, 0, rawData.Length); BZip2.Close(); return(ms.ToArray()); }
/// <summary> /// 压缩 /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] Compress(byte[] input) { using (MemoryStream outputStream = new MemoryStream()) { using (BZip2OutputStream zipStream = new BZip2OutputStream(outputStream)) { zipStream.Write(input, 0, input.Length); zipStream.Close(); } return(outputStream.ToArray()); } }
public static byte[] ZipStream(byte[] sBuffer) { MemoryStream m_msBZip2 = null; BZip2OutputStream m_osBZip2 = null; byte[] result; try { m_msBZip2 = new MemoryStream(); Int32 size = sBuffer.Length; using (BinaryWriter writer = new BinaryWriter(m_msBZip2, System.Text.Encoding.ASCII)) { writer.Write(size); m_osBZip2 = new BZip2OutputStream(m_msBZip2); m_osBZip2.Write(sBuffer, 0, sBuffer.Length); m_osBZip2.Close(); result = m_msBZip2.ToArray(); m_msBZip2.Close(); writer.Close(); } } finally { if (m_osBZip2 != null) { m_osBZip2.Dispose(); } if (m_msBZip2 != null) { m_msBZip2.Dispose(); } } return(result); }
/// <summary> /// 压缩 /// </summary> /// <param name="input"></param> /// <returns></returns> public static string Compress(string input) { byte[] buffer = Encoding.UTF8.GetBytes(input); using (MemoryStream outputStream = new MemoryStream()) { using (BZip2OutputStream zipStream = new BZip2OutputStream(outputStream)) { zipStream.Write(buffer, 0, buffer.Length); zipStream.Close(); } return(Convert.ToBase64String(outputStream.ToArray())); } }
public static void SerializeToFileBZip2(T data, string fileName) { FileStream fs = new FileStream(fileName, FileMode.Create); BZip2OutputStream bzs = new BZip2OutputStream(fs); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(bzs, data); bzs.Close(); bzs.Dispose(); fs.Close(); fs.Dispose(); }
public static byte[] SerializeToBytesBZip2(T data) { MemoryStream ms = new MemoryStream(); BZip2OutputStream gzs = new BZip2OutputStream(ms); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(gzs, data); gzs.Close(); gzs.Dispose(); byte[] ret = ms.ToArray(); ms.Close(); ms.Dispose(); return(ret); }
public static byte[] CatHomeMain(byte[] data, byte[] home, byte[] info, bool isCompress = false) { MemoryStream memoryStream = null; CryptoStream cryptoStream = null; byte[] result; try { var transform = new RijndaelManaged { Padding = PaddingMode.PKCS7, Mode = CipherMode.CBC, KeySize = 256, BlockSize = 256 }.CreateEncryptor(home, info); memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write); if (isCompress) { var bzip2OutputStream = new BZip2OutputStream(cryptoStream, 1); bzip2OutputStream.Write(data, 0, data.Length); bzip2OutputStream.Close(); } else { cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); } result = memoryStream.ToArray(); } catch (Exception) { result = null; } finally { if (memoryStream != null) { memoryStream.Close(); } if (cryptoStream != null) { cryptoStream.Close(); } } return(result); }
/// <summary> bzip 压缩算法 速度奇慢 压缩比高一丁点 /// 压缩文件 /// </summary> /// <param name="fileToCompress"></param> public static void CompressFileBzip(string inputPath, string outPath) { FileStream inputStream = new FileStream(inputPath, FileMode.Open); //string result = string.Empty; FileStream outputStream = new FileStream(outPath, FileMode.Create); using (BZip2OutputStream zipStream = new BZip2OutputStream(outputStream)) { CopyStream(inputStream, zipStream); zipStream.Close(); } inputStream.Close(); outputStream.Close(); }
/// <summary> /// Compress <paramref name="instream">input stream</paramref> sending /// result to <paramref name="outputstream">output stream</paramref> /// </summary> public static void Compress(Stream instream, Stream outstream, int blockSize) { System.IO.Stream bos = outstream; System.IO.Stream bis = instream; int ch = bis.ReadByte(); BZip2OutputStream bzos = new BZip2OutputStream(bos, blockSize); while (ch != -1) { bzos.WriteByte((byte)ch); ch = bis.ReadByte(); } bis.Close(); bzos.Close(); }
public static byte[] ZipCompress(byte[] byte_0) { MemoryStream stream = new MemoryStream(); Stream stream2 = new BZip2OutputStream(stream); try { stream2.Write(byte_0, 0, byte_0.Length); } finally { stream2.Close(); stream.Close(); } return(stream.ToArray()); }
public static byte[] CatHomeMain(byte[] data, byte[] home, byte[] info, bool isPress = false) { MemoryStream stream = null; CryptoStream stream2 = null; byte[] buffer; try { ICryptoTransform transform = new RijndaelManaged { Padding = PaddingMode.PKCS7, Mode = CipherMode.CBC, KeySize = 0x100, BlockSize = 0x100 }.CreateEncryptor(home, info); stream = new MemoryStream(); stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write); if (isPress) { BZip2OutputStream stream3 = new BZip2OutputStream(stream2); stream3.Write(data, 0, data.Length); stream3.Close(); } else { stream2.Write(data, 0, data.Length); stream2.FlushFinalBlock(); } buffer = stream.ToArray(); } catch (Exception) { buffer = null; } finally { if (stream != null) { stream.Close(); } if (stream2 != null) { stream2.Close(); } } return(buffer); }
/// <summary> /// Compress <paramref name="instream">input stream</paramref> sending /// result to <paramref name="outputstream">output stream</paramref> /// </summary> public static long Compress(Stream instream, Stream outstream, int blockSize) { System.IO.Stream bos = outstream; System.IO.Stream bis = instream; int ch; BZip2OutputStream bzos = new BZip2OutputStream(bos, blockSize); byte[] buffer = new byte[512]; while ((ch = bis.Read(buffer, 0, buffer.Length)) != 0) { bzos.Write(buffer, 0, ch); } bis.Close(); bzos.Flush(); bzos.Close(); return(bzos.Size); }
/*static int GetMaterialIndex(Material material) * { * if (!_materialPack.Contains(material)) * _materialPack.Add(material); * return _materialPack.IndexOf(material); * }*/ /*static int GetTextureIndex(Texture txtr) * { * if (_texturePack == null) * return -1; * if (!_texturePack.Contains(txtr)) * _texturePack.Add(txtr); * return _texturePack.IndexOf(txtr); * }*/ /*byte[] GetResource(object value) * { * if (!_objectPack.Contains(value)) * { * _objectPack.Add(value); * _bytesPack.Add(Encoding(value)); * } * var result = new byte[]{252,0,0,0,0}; * Buffer.BlockCopy(EncodeInteger(_objectPack.IndexOf(value)), 0, result, 1, 4); * return result; * }*/ /// <summary> /// Compress byte array /// For decompress use Decode /// /// </summary> /// <param name="data">array of byte</param> /// <returns></returns> public byte[] Compress(byte[] data) { MemoryStream mMsBZip2 = null; BZip2OutputStream mOsBZip2 = null; byte[] result; try { mMsBZip2 = new MemoryStream(); mOsBZip2 = new BZip2OutputStream(mMsBZip2); mOsBZip2.Write(data, 0, data.Length); mOsBZip2.Close(); var packLength = mMsBZip2.ToArray().Length; var unpackLength = data.Length; result = new byte[11 + packLength]; Buffer.SetByte(result, 0, Version); Buffer.SetByte(result, 1, 0x0); //Size of next head (bytes) #region Head //reserved //Buffer.SetByte(result, 2, 0x0);example head #endregion Buffer.SetByte(result, 2, 0xfe); //Compress Buffer.BlockCopy(BitConverter.GetBytes(packLength), 0, result, 3, 4); Buffer.BlockCopy(BitConverter.GetBytes(unpackLength), 0, result, 7, 4); Buffer.BlockCopy(mMsBZip2.ToArray(), 0, result, 11, packLength); mMsBZip2.Close(); mMsBZip2.Dispose(); } finally { if (mOsBZip2 != null) { mOsBZip2.Dispose(); } if (mMsBZip2 != null) { mMsBZip2.Dispose(); } } return(result); }
public byte[] Compress(byte[] data, UInt32 size) { byte[] result; Stream inStream = new MemoryStream(data); Stream outStream = new MemoryStream((int)size); try { using (BZip2OutputStream bzipOutput = new BZip2OutputStream(outStream, 9)) { bzipOutput.IsStreamOwner = false; StreamUtils.Copy(inStream, bzipOutput, new byte[4096]); bzipOutput.Close(); result = (outStream as MemoryStream).ToArray(); } } catch (Exception e) { result = null; } finally { inStream.Close(); outStream.Close(); } return(result); }
public static byte[] ZipString(string sBuffer) { MemoryStream msCompressed = new MemoryStream(); byte[] bytesBuffer = Encoding.ASCII.GetBytes(sBuffer); Int32 size = bytesBuffer.Length; // Prepend the compressed data with the length of the uncompressed data (firs 4 bytes) BinaryWriter writer = new BinaryWriter(msCompressed, System.Text.Encoding.ASCII); writer.Write(size); // compress the data BZip2OutputStream zosCompressed = new BZip2OutputStream(msCompressed); zosCompressed.Write(bytesBuffer, 0, size); //zosCompressed.Finalize(); zosCompressed.Close(); bytesBuffer = msCompressed.ToArray(); return(bytesBuffer); }