public Read ( byte buffer, int offset, int count ) : int | ||
buffer | byte | Array of bytes to store values in |
offset | int | Offset in array to begin storing data |
count | int | The maximum number of bytes to read |
return | int |
public void CreateEmptyArchive() { MemoryStream ms = new MemoryStream(); BZip2OutputStream outStream = new BZip2OutputStream(ms); outStream.Close(); ms = new MemoryStream(ms.GetBuffer()); ms.Seek(0, SeekOrigin.Begin); 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; } Assertion.AssertEquals(pos, 0); }
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); } }
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); 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) { Assertion.AssertEquals(buf2[i], buf[i]); } }
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 static byte[] Decompress(byte[] input, int uncompressedLength) { Contract.Requires(input != null); Contract.Requires(uncompressedLength >= 0); Contract.Ensures(Contract.Result<byte[]>() != null); Contract.Ensures(Contract.Result<byte[]>().Length == uncompressedLength); var output = new byte[uncompressedLength]; using (var stream = new BZip2InputStream(new MemoryStream(input, false))) stream.Read(output, 0, uncompressedLength); return output; }
/// <summary> /// 解压缩字符串 /// </summary> /// <param name="str">需要解压的base64位字符串</param> /// <returns>字符串</returns> public string UnCompressStr(string str) { //判断需解压字符串是否为空 if (str == null) { //如果为空,返回空 return(""); } //如果不为空则执行以下语句 try { //定义字符串数组,获取需解压字符串数据包 string[] temp = str.Split(' '); //如果字符小于2返回空 if (temp.Length < 2) { return(""); } //调用文本可变字符串类 System.Text.StringBuilder uncompressedString = new System.Text.StringBuilder(); int totalLength = 0; //base64位字符串数字转化为8位字符数组 byte[] bytInput = System.Convert.FromBase64String(temp[0]); //转化为等效32位字符数组 byte[] writeData = new byte[Convert.ToInt32(temp[1])]; //构建读取流 Stream s2 = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new MemoryStream(bytInput)); //循环解压文本 while (true) { int size = s2.Read(writeData, 0, writeData.Length); if (size > 0) { totalLength += size; uncompressedString.Append(System.Text.Encoding.Unicode.GetString(writeData, 0, size)); //返回解压后文本 return(uncompressedString.ToString()); } return(""); } } catch (Exception ex) { throw ex; } }
public static string UnzipString(string compbytes) { string result; MemoryStream m_msBZip2 = null; BZip2InputStream m_isBZip2 = null; try { m_msBZip2 = new MemoryStream(Convert.FromBase64String(compbytes)); // read final uncompressed string size stored in first 4 bytes // using (BinaryReader reader = new BinaryReader(m_msBZip2, System.Text.Encoding.ASCII)) { Int32 size = reader.ReadInt32(); m_isBZip2 = new BZip2InputStream(m_msBZip2); byte[] bytesUncompressed = new byte[size]; m_isBZip2.Read(bytesUncompressed, 0, bytesUncompressed.Length); m_isBZip2.Close(); m_msBZip2.Close(); result = Encoding.ASCII.GetString(bytesUncompressed); reader.Close(); } } finally { if (m_isBZip2 != null) { m_isBZip2.Dispose(); } if (m_msBZip2 != null) { m_msBZip2.Dispose(); } } return result; }
// TODO: Create a class Archive.cs and do all the archiving stuff there! public static bool UncompressBzip2File(string Filename, string To, Gtk.ProgressBar bar) { try{ Console.WriteLine(Filename); BZip2InputStream bzipIn = new BZip2InputStream(File.OpenRead(Filename)); FileStream streamWriter = File.Create(To+Path.GetFileNameWithoutExtension(Filename)); long size=0; byte[] data = new byte[1024]; while (true) { size = bzipIn.Read(data, 0, data.Length); if (size > 0) streamWriter.Write(data, 0, (int) size); else break; } streamWriter.Close(); Console.WriteLine("Deflating the gzip file done!"); return true; } catch(Exception e) { Console.WriteLine("An exception occured while deflating the bzip2 file: "+e.Message); return false; } }
/// <summary> /// required for applying a binary patch /// </summary> private Int32 loopread(ref BZip2InputStream zipStream, ref byte[] buf, Int32 offset, Int32 nbytes) { Int32 ptr; Int32 lenread; ptr = 0; while (ptr < nbytes) { lenread = zipStream.Read(buf, offset + ptr, nbytes); if (lenread == 0) { return ptr; } if (lenread == -1) { return -1; } ptr = ptr + lenread; } return ptr; }
public static SteamPacket ReassemblePacket(List<byte[]> splitPackets, bool isCompressed, short uncompressedSize, int packetChecksum) { byte[] packetData, tmpData; packetData = new byte[0]; foreach(byte[] splitPacket in splitPackets) { if(splitPacket == null) { throw new Exception(); } tmpData = packetData; packetData = new byte[tmpData.Length + splitPacket.Length]; MemoryStream memStream = new MemoryStream(packetData); memStream.Write(tmpData, 0, tmpData.Length); memStream.Write(splitPacket, 0, splitPacket.Length); } if(isCompressed) { BZip2InputStream bzip2 = new BZip2InputStream(new MemoryStream(packetData)); bzip2.Read(packetData, 0, uncompressedSize); Crc32 crc32 = new Crc32(); crc32.Update(packetData); if(crc32.Value != packetChecksum) { throw new Exception("CRC32 checksum mismatch of uncompressed packet data."); } } return SteamPacket.CreatePacket(packetData); }
public byte[] DeCompress(byte[] bytesToDecompress) { //Sets up the write data byte array (acts as a buffer array for the decompression, in 4096 byte blocks) byte[] writeData = new byte[4096]; //Set up the initial Stream object (used a compact method by creating the memory stream form the byte-array and passing in all in one) Stream s2 = new BZip2InputStream(new MemoryStream(bytesToDecompress)); //Stringbuilder which will hold the results of the decompression MemoryStream outStream = new MemoryStream(); while(true) { //The business end, pops the next 4096 bytes from the Stream into the writeData buffer int size = s2.Read(writeData,0,writeData.Length); if(size>0) { outStream.Write(writeData,0,size); } else { break; } } s2.Close(); byte[] outArr = outStream.ToArray(); outStream.Close(); return outArr; }
private string PassxDecode(string passx) { using (var input = new MemoryStream ()) { var passx_base64_decoded = Convert.FromBase64String (passx); input.Write (passx_base64_decoded, 0, passx_base64_decoded.Length); input.Seek (0, SeekOrigin.Begin); using (var output = new BZip2InputStream (input)) { var passx_bzip2_decoded = new byte[1024]; output.Read (passx_bzip2_decoded, 0, passx_bzip2_decoded.Length); return System.Text.Encoding.UTF8.GetString (passx_bzip2_decoded); } } return null; }
/// <summary> /// Decompresses a compressed string to a byte array /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] DecompressBytes(this byte[] data) { // exit if null if (data.IsNullOrEmpty()) return null; using (MemoryStream msBZip2 = new MemoryStream(data)) { // read final uncompressed string size stored in first 4 bytes using (BinaryReader reader = new BinaryReader(msBZip2)) { int size = reader.ReadInt32(); using (BZip2InputStream m_isBZip2 = new BZip2InputStream(msBZip2)) { byte[] bytesUncompressed = new byte[size]; m_isBZip2.Read(bytesUncompressed, 0, size); return bytesUncompressed; } } } }
public byte[] UncompressBuffer(ref byte[] buffer) { MemoryStream Compressed = new MemoryStream(buffer); MemoryStream UnCompressed = new MemoryStream(); try { BZip2InputStream zisUncompressed = new BZip2InputStream(Compressed); int size; byte[] data = new byte[4096]; do { size = zisUncompressed.Read(data, 0, data.Length); UnCompressed.Write(data, 0, size); }while (size > 0); zisUncompressed.Close(); Compressed.Close(); byte []bUncompressedData = UnCompressed.GetBuffer(); UnCompressed.Close(); return bUncompressedData; } catch(Exception ex) { WebMeeting.Client.ClientUI.getInstance().ShowExceptionMessage("Module ::: AppShare public byte[] UncompressBuffer(ref byte[] buffer)",ex,"",false); } return null; }
public static int DecompressBlock(byte[] inBuffer, int inLength, byte[] outBuffer, bool multi) { byte[] tempBuffer; if (!multi) return DclCompression.DecompressBlock(inBuffer, 0, inLength, outBuffer); else // Examinate first byte for finding compression methods used { switch (inBuffer[0]) { case 0x01: // Huffman throw new MpqCompressionNotSupportedException(0x01, "Huffman"); case 0x02: // Zlib (Deflate/Inflate) #if USE_SHARPZIPLIB // Use SharpZipLib's Deflate implementation Inflater.Reset(); // The first property read will initialize the field… inflater.SetInput(inBuffer, 1, inLength - 1); return inflater.Inflate(outBuffer); #else // Use .NET 2.0's built-in inflate algorithm using (var inStream = new MemoryStream(inBuffer, 3, inLength - 7, false, false)) using (var outStream = new DeflateStream(inStream, CompressionMode.Decompress)) outStream.Read(outBuffer, 0, outBuffer.Length); #endif case 0x08: // PKWare DCL (Implode/Explode) return DclCompression.DecompressBlock(inBuffer, 1, inLength - 1, outBuffer); case 0x10: // BZip2 #if USE_SHARPZIPLIB // Use SharpZipLib for decompression using (var inStream = new MemoryStream(inBuffer, 1, inLength - 1, false, false)) using (var outStream = new BZip2InputStream(inStream)) return outStream.Read(outBuffer, 0, outBuffer.Length); #else throw new CompressionNotSupportedException("BZip2"); #endif case 0x12: // LZMA using (var inStream = new MemoryStream(inBuffer, 1, inLength - 1, false, false)) using (var outStream = new MemoryStream(outBuffer, true)) { lzmaDecoder.Code(inStream, outStream, inStream.Length, outStream.Length, null); return checked((int)outStream.Position); } case 0x20: // Sparse return SparseCompression.DecompressBlock(inBuffer, 1, inLength - 1, outBuffer); case 0x22: // Sparse + Deflate #if USE_SHARPZIPLIB // Use SharpZipLib's Deflate implementation Inflater.Reset(); // The first property read will initialize the field… inflater.SetInput(inBuffer, 1, inLength - 1); tempBuffer = CommonMethods.GetSharedBuffer(outBuffer.Length); return SparseCompression.DecompressBlock(tempBuffer, 0, inflater.Inflate(tempBuffer), outBuffer); #else // Use .NET 2.0's built-in inflate algorithm using (var inStream = new MemoryStream(inBuffer, 3, inLength - 7, false, false)) using (var inoutStream = new DeflateStream(inStream, CompressionMode.Decompress)) using (var outStream = new SparseInputStream(inoutStream)) return outStream.Read(outBuffer, 0, outBuffer.Length); #endif case 0x30: // Sparse + BZip2 #if USE_SHARPZIPLIB // Use SharpZipLib for decompression using (var inStream = new MemoryStream(inBuffer, 1, inLength - 1, false, false)) using (var inoutStream = new BZip2InputStream(inStream)) using (var outStream = new SparseInputStream(inoutStream)) return outStream.Read(outBuffer, 0, outBuffer.Length); #else throw new CompressionNotSupportedException("Sparse + BZip2"); #endif case 0x40: // Mono IMA ADPCM throw new MpqCompressionNotSupportedException(0x40, "Mono IMA ADPCM"); case 0x41: // Mono IMA ADPCM + Huffman throw new MpqCompressionNotSupportedException(0x41, "Mono IMA ADPCM + Huffman"); case 0x48: // Mono IMA ADPCM + Implode throw new MpqCompressionNotSupportedException(0x48, "Mono IMA ADPCM + Implode"); case 0x80: // Stereo IMA ADPCM throw new MpqCompressionNotSupportedException(0x80, "Stereo IMA ADPCM"); case 0x81: // Stereo IMA ADPCM + Huffman throw new MpqCompressionNotSupportedException(0x81, "Stereo IMA ADPCM + Huffman"); case 0x88: // Stereo IMA ADPCM + Implode throw new MpqCompressionNotSupportedException(0x88, "Stereo IMA ADPCM + Implode"); default: throw new MpqCompressionNotSupportedException(inBuffer[0]); } } }
/// <summary> /// Processes a file buffer. /// Will decompress, and find an appropriate extension for it if possible. /// </summary> private static void ProcessFile(ref byte[] buffer, out string extension) { extension = ""; //decompress gzip if (buffer.Length > 5 && (buffer[4] << 8) + buffer[5] == 0x1f8b) //gzip { //remove another 4 non-file bytes var tempBuffer = new byte[buffer.Length - 4]; Array.Copy(buffer, 4, tempBuffer, 0, buffer.Length - 4); buffer = tempBuffer; var decompressionStream = new GZipStream(new MemoryStream(buffer), CompressionMode.Decompress); int readBytes; tempBuffer = new byte[0]; do { var readBuffer = new byte[100000]; readBytes = decompressionStream.Read(readBuffer, 0, 100000); var storedBytes = tempBuffer.Length; Array.Resize(ref tempBuffer, tempBuffer.Length + readBytes); Array.Copy(readBuffer, 0, tempBuffer, storedBytes, readBytes); } while (readBytes == 100000); buffer = tempBuffer; } //decompress bzip2 if (buffer.Length > 9 && buffer[4] == 0x31 && buffer[5] == 0x41 && buffer[6] == 0x59 && buffer[7] == 0x26 && buffer[8] == 0x53 && buffer[9] == 0x59) //bzip2 { //remove another 4 non-file bytes var tempBuffer = new byte[buffer.Length - 4]; Array.Copy(buffer, 4, tempBuffer, 0, buffer.Length - 4); buffer = tempBuffer; //prepend file header byte[] magic = { 0x42, 0x5a, //BZ (signature) 0x68, //h (version) 0x31 //*100kB block-size }; tempBuffer = new byte[magic.Length + buffer.Length]; magic.CopyTo(tempBuffer, 0); buffer.CopyTo(tempBuffer, magic.Length); buffer = tempBuffer; var decompressionStream = new BZip2InputStream(new MemoryStream(buffer)); int readBytes; tempBuffer = new byte[0]; do { var readBuffer = new byte[100000]; readBytes = decompressionStream.Read(readBuffer, 0, 100000); var storedBytes = tempBuffer.Length; Array.Resize(ref tempBuffer, tempBuffer.Length + readBytes); Array.Copy(readBuffer, 0, tempBuffer, storedBytes, readBytes); } while (readBytes == 100000); buffer = tempBuffer; } //detect appropriate extension if (buffer.Length > 3 && (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3] == 0x4f676753) extension = ".ogg"; else if (buffer.Length > 3 && (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3] == 0x4a414741) extension = ".jaga"; else if (buffer.Length > 3 && (uint)(buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3] == 0x89504e47) extension = ".png"; }