public static byte[] DecompressBZip2(byte[] InData) { using (MemoryStream ms = new MemoryStream(InData)) { using (BZip2InputStream input = new BZip2InputStream(ms, false)) { using (MemoryStream decompressedData = new MemoryStream()) { input.CopyTo(decompressedData); return decompressedData.ToArray(); } } } }
public static MemoryStream DecompressFromFile(string targetFilePath, bool isWritable) { MemoryStream ms; FileStream fs = null; try { fs = new FileStream(targetFilePath, FileMode.Open); var tmp = new byte[4]; fs.Read(tmp, 0, 4); int decompressedLength = BitConverter.ToInt32(tmp, 0); var b = new byte[decompressedLength]; using (BZip2InputStream gzs = new BZip2InputStream(fs)) //ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(fs)) { fs = null; // Read decompressed data gzs.Read(b, 0, b.Length); } ms = new MemoryStream(b, 0, b.Length, isWritable, true); } finally { if (fs != null) { fs.Dispose(); } } return(ms); }
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); } }
private static void Download(string source, string target, string filename) { var sourceUrl = source + "/" + filename + ".bz2"; var targetZip = Path.Combine(target, filename + ".bz2"); var targetFile = Path.Combine(target, filename); if (File.Exists(targetFile)) return; // ReSharper disable once AssignNullToNotNullAttribute Directory.CreateDirectory(Path.GetDirectoryName(targetFile)); Console.WriteLine("Downloading '{0}'", sourceUrl); if (!File.Exists(targetZip)) { using (var wc = new WebClient()) { wc.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; wc.DownloadFile(sourceUrl, targetZip); } } using (var istream = File.OpenRead(targetZip)) using (var zstream = new BZip2InputStream(istream)) using (var ostream = File.OpenWrite(targetFile)) { CopyStream(zstream, ostream); } File.Delete(targetZip); }
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 bool LoadFromComp(byte[] gzData) { MemoryStream memoryStream = new MemoryStream(gzData); string strText = null; try { using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8)) { int num = binaryReader.ReadInt32(); BZip2InputStream bZip2InputStream = new BZip2InputStream(memoryStream); byte[] array = new byte[num]; bZip2InputStream.Read(array, 0, array.Length); bZip2InputStream.Close(); strText = Encoding.UTF8.GetString(array); memoryStream.Close(); binaryReader.Close(); } } catch (Exception arg) { this._OutputDebug(string.Format("The process failed: {0}", arg)); return(false); } return(this.LoadFromText(strText)); }
public static Bytes decompress([BytesConversion]IList<byte> data) { if (data.Count == 0) { return new Bytes(); } byte[] buffer = new byte[1024]; using (var output = new MemoryStream()) { using (var input = new MemoryStream(data.ToArrayNoCopy(), false)) { using (var bz2 = new BZip2InputStream(input)) { int read = 0; while(true) { try { read = bz2.Read(buffer, 0, buffer.Length); } catch (IOException e) { throw PythonOps.ValueError(e.Message); } if (read > 0) { output.Write(buffer, 0, read); } else { break; } } } } return Bytes.Make(output.ToArray()); } }
public void Performance() { window_ = new WindowedStream(0x150000); outStream_ = new BZip2OutputStream(window_, 1); const long Target = 0x10000000; readTarget_ = writeTarget_ = Target; Thread reader = new Thread(Reader); reader.Name = "Reader"; Thread writer = new Thread(Writer); writer.Name = "Writer"; DateTime startTime = DateTime.Now; writer.Start(); inStream_ = new BZip2InputStream(window_); reader.Start(); Assert.IsTrue(writer.Join((int)TimeSpan.FromMinutes(5.0D).TotalMilliseconds)); Assert.IsTrue(reader.Join((int)TimeSpan.FromMinutes(5.0D).TotalMilliseconds)); DateTime endTime = DateTime.Now; TimeSpan span = endTime - startTime; Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (Target / 1024) / span.TotalSeconds); }
// Constructors ----------------------------------- public XmlDumpFileReader(string filePath) { Stream fileStream = File.OpenRead(filePath); var decompressedStream = new BZip2InputStream(fileStream); _reader = XmlReader.Create(decompressedStream); }
protected override BitwiseStream internalEncode(BitwiseStream data) { BitStream ret = new BitStream(); using (var bzip2 = new BZip2InputStream(data, true)) { try { // For some reason, Ionic decided the BZip2InputStream // should return -1 from Read() when EOF is reached. This // breaks Stream.CopyTo() as it expects 0 on EOF. // We need to use ReadByte() instead. int val; while ((val = bzip2.ReadByte()) != -1) { ret.WriteByte((byte)val); } } catch (Exception ex) { throw new SoftException("Could not BZip decompress data.", ex); } } ret.Seek(0, SeekOrigin.Begin); return(ret); }
/// <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 bool openTrace(string folderPath, string filename) { try { FileInfo fileInfo = new FileInfo(folderPath + filename + ".bz2"); inputFileStream = fileInfo.OpenRead(); bzip2InputStream = new BZip2InputStream(inputFileStream); } catch { return(false); } numberOfBranches = 0; for (int i = 0; i < BRANCH_MEMORY_NUMBER_OF_LINES; i++) { for (int j = 0; j < BRANCH_MEMORY_ASSOCIATIVITY_SIZE; j++) { branchesMemory[i][j].clear(); } } for (int i = 0; i < 8; i++) { classMisspredicted[i] = 0; } initReturnAddressStack(); now = 0; return(true); }
/// <summary> /// Create bzip2 file provider from byte[]. /// /// Has one entry by name of <paramref name="entryName"/>. /// Reads the whole stream once just to get the entry length. /// </summary> /// <param name="data"></param> /// <param name="entryName"></param> /// <param name="hintPath">(optional) clue of the file that is being opened</param> /// <param name="lastModified">Date time for folder entries</param> /// <exception cref="IOException"></exception> public BZip2FileProvider(byte[] data, string entryName, string hintPath = null, DateTimeOffset?lastModified = default) : base(hintPath, lastModified) { if (data == null) { throw new ArgumentNullException(nameof(data)); } // Mutable long, and object to take closure reference to. long[] lengthContainer = new long[] { -1 }; Stream opener() { MemoryStream ms = new MemoryStream(data); try { BZip2InputStream bzis = new BZip2InputStream(ms) { IsStreamOwner = true }; return(lengthContainer[0] < 0L ? bzis : (Stream) new BZip2StreamFix(bzis, null, null, lengthContainer[0])); } catch (Exception) when(_closeStream(ms)) { throw new IOException($"Failed to read .bzip2 from byte[]"); } } // Calculate length by reading the whole thing. lengthContainer[0] = CalculateLength(opener); this.streamProvider = new StreamOpener(opener, entryName, belatedDisposeList); this.root.files[entryName] = new ArchiveFileEntry(this.streamProvider, entryName, entryName, lengthContainer[0], lastModified ?? DateTimeOffset.MinValue); }
private void ReadFile() { var buffer = new byte[1024 * 1024];// more than big enough for all files using (var bz2 = new BZip2InputStream(File.Open(_path, FileMode.Open))) using (var tar = new TarInputStream(bz2)) { TarEntry entry; while ((entry = tar.GetNextEntry()) != null) { if (entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING") { continue; } var readSoFar = 0; while (true) { var bytes = tar.Read(buffer, readSoFar, ((int)entry.Size) - readSoFar); if (bytes == 0) { break; } readSoFar += bytes; } // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff // so we can reads the values properly var fileText = new StreamReader(new MemoryStream(buffer, 0, readSoFar)).ReadToEnd(); _entries.Add(fileText); Interlocked.Increment(ref reads); } } _entries.Add(null); }
// Token: 0x06000023 RID: 35 RVA: 0x0000308C File Offset: 0x0000128C public static byte[] BZipDeCompress(byte[] data, bool isClearData = true) { byte[] result = null; using (MemoryStream memoryStream = new MemoryStream()) { using (MemoryStream memoryStream2 = new MemoryStream(data)) { using (Stream stream = new BZip2InputStream(memoryStream2)) { stream.Flush(); byte[] array = new byte[2048]; int count; while ((count = stream.Read(array, 0, array.Length)) > 0) { memoryStream.Write(array, 0, count); } } } result = memoryStream.ToArray(); } if (isClearData) { Array.Clear(data, 0, data.Length); } return(result); }
public static SteamPacket ReassemblePacket(List <byte[]> splitPackets, bool isCompressed, short uncompressedSize, int packetChecksum) { byte[] packetData; packetData = new byte[0]; MemoryStream memStream = new MemoryStream(); foreach (byte[] splitPacket in splitPackets) { 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(memStream.ToArray())); }
private static List <string> ExtractLibraries(IEnumerable <string> resourceNames, string targetDir) { var filePaths = new List <string>(); foreach (var resourceName in resourceNames) { var fileName = Path.GetFileNameWithoutExtension( EmbeddedResources.ResourceNameToFileName(resourceName) ) + EmbeddedResources.GetNativeExtensionForCurrentPlatform(); var libraryPath = Path.Combine(targetDir, fileName); filePaths.Add(libraryPath); var embeddedPackageStream = EmbeddedResources.GetResourceStream(resourceName); var bzipStream = new BZip2InputStream(embeddedPackageStream); if (File.Exists(libraryPath)) { if (ModuleInitializer.BootConfig.SkipChecksumVerification) { continue; } var memoryStream = new MemoryStream(); bzipStream.CopyTo(memoryStream, 1024); var memoryBuffer = memoryStream.ToArray(); var embeddedLibraryBytes = memoryBuffer[0..(int)memoryStream.Position];
public static void FromFile(string path, Encoding encoding) { Console.OutputEncoding = encoding; var input = File.OpenRead(path); var output = new BZip2InputStream(input); var streamReader = new StreamReader(output, encoding); var counter = 0; string line = streamReader.ReadLine(); while (!string.IsNullOrEmpty(line)) { ProcessOneLine(line); line = streamReader.ReadLine(); counter++; if (counter % 100_000 == 0) { Console.WriteLine($"[{DateTime.UtcNow}]: {counter} records were processed. There are {persons.Count} with {names.Count} names and {nameParts.Count} name parts"); } } Console.WriteLine($"{counter} records were processed. There are {persons.Count} with {names.Count} names and {nameParts.Count} name parts. {errors.Count} was caught"); Console.WriteLine($"Ended at {DateTime.UtcNow}"); Task.WaitAll( FileStorage.SavePersons(encoding), FileStorage.SaveNames(encoding), FileStorage.SaveNameParts(encoding), FileStorage.SaveErrors(encoding)); }
private static void Download(string source, string target, string filename) { var sourceUrl = source + "/" + filename + ".bz2"; var targetZip = Path.Combine(target, filename + ".bz2"); var targetFile = Path.Combine(target, filename); if (File.Exists(targetFile)) { return; } // ReSharper disable once AssignNullToNotNullAttribute Directory.CreateDirectory(Path.GetDirectoryName(targetFile)); Console.WriteLine("Downloading '{0}'", sourceUrl); if (!File.Exists(targetZip)) { using (var wc = new WebClient()) { wc.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; wc.DownloadFile(sourceUrl, targetZip); } } using (var istream = File.OpenRead(targetZip)) using (var zstream = new BZip2InputStream(istream)) using (var ostream = File.OpenWrite(targetFile)) { CopyStream(zstream, ostream); } File.Delete(targetZip); }
static void Decompress(string srcfile, ProgressBar bar = null) { if (bar == null) { ConsoleExt.Log("Decompressing {0}.", Path.GetFileName(srcfile)); } bar.Message = string.Format("Decompressing {0}.", Path.GetFileName(srcfile)); using (var stream = new BZip2InputStream(new FileStream(srcfile, FileMode.Open))) { using (var file = File.Create(Path.Combine(Path.GetDirectoryName(srcfile), Path.GetFileNameWithoutExtension(srcfile)))) { var buffer = new byte[2048]; int n; while ((n = stream.Read(buffer, 0, buffer.Length)) > 0) { file.Write(buffer, 0, n); } } } if (bar == null) { ConsoleExt.Log("Decompressed {0}.", Path.GetFileName(srcfile)); ConsoleExt.Log("Deleting {0}", Path.GetFileName(srcfile)); } bar.Message = string.Format("Decompressed {0}, deleting.", Path.GetFileName(srcfile)); bar.Destroy = true; File.Delete(srcfile); }
// // // public MemoryStream Decompress(Stream stream) { if (stream == null) { return(null); } byte[] buffer = new byte[BUFFER_SIZE]; MemoryStream outStream = new MemoryStream(); using (BZip2InputStream bz2Stream = new BZip2InputStream(stream)) { while (true) { int bytesRead = bz2Stream.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { break; } outStream.Write(buffer, 0, bytesRead); } outStream.Flush(); } outStream.Position = 0; return(outStream); }
private object UnzipBytes(byte[] compbytes, ref int startPos) { //Debug.Log("UNZIP"); var result = new byte[0]; var spos = startPos; //if (UNP.Registered) //{ var packLength = DecodeInteger(compbytes, ref spos); var unpackLength = DecodeInteger(compbytes, ref spos); //try //{ //Debug.Log("Unpack: "+unpackLength.ToString()+" Pack: "+packLength.ToString()); var buffer = new byte[packLength]; Buffer.BlockCopy(compbytes, spos, buffer, 0, packLength); var msUncompressed = new MemoryStream(buffer); var zisUncompressed = new BZip2InputStream(msUncompressed); result = new byte[unpackLength]; zisUncompressed.Read(result, 0, unpackLength); zisUncompressed.Close(); msUncompressed.Close(); startPos += 8 + packLength; //} //else //{ // Debug.LogError("Error REG U " + className + " " + methodName); //} return(Decode(result)); }
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 LuaFunction load(string modulename) { string lua_chunk = ""; string filename = modulename + ".lua"; in_stream_.Position = 0; // rewind Stream gzipStream = new BZip2InputStream(in_stream_); TarInputStream tar = new TarInputStream(gzipStream); TarEntry tarEntry; LuaFunction func = null; while ((tarEntry = tar.GetNextEntry()) != null) { if (tarEntry.IsDirectory) { continue; } if (filename == tarEntry.Name) { MemoryStream out_stream = new MemoryStream(); tar.CopyEntryContents(out_stream); out_stream.Position = 0; // rewind StreamReader stream_reader = new StreamReader(out_stream); lua_chunk = stream_reader.ReadToEnd(); func = lua_.LoadString(lua_chunk, modulename); string dum = func.ToString(); break; } } return func; }
public static Bytes decompress([BytesConversion] IList <byte> data) { if (data.Count == 0) { return(new Bytes()); } byte[] buffer = new byte[1024]; using (var output = new MemoryStream()) { using (var input = new MemoryStream(data.ToArrayNoCopy(), false)) { using (var bz2 = new BZip2InputStream(input)) { int read = 0; while (true) { try { read = bz2.Read(buffer, 0, buffer.Length); } catch (IOException e) { throw PythonOps.ValueError(e.Message); } if (read > 0) { output.Write(buffer, 0, read); } else { break; } } } } return(Bytes.Make(output.ToArray())); } }
public DataBuffer ExtractFile(int k) { if (k >= FileCount || k < 0) { return(null); } if (FileBuffers[k] != null) { return(new DataBuffer(FileBuffers[k])); } FileBuffers[k] = new byte[UncompressedFileSizes[k]]; if (!ArchiveDecompressed) { byte[] temp = new byte[CompressedFileSizes[k]]; FileBuffers[k] = new byte[UncompressedFileSizes[k]]; Array.Copy(MainBuffer, FileLocations[k], temp, 0, temp.Length); MemoryStream ms = new MemoryStream(temp); BZip2InputStream bz = new BZip2InputStream(ms); bz.Read(FileBuffers[k], 0, UncompressedFileSizes[k]); } else { Array.Copy(MainBuffer, FileLocations[k], FileBuffers[k], 0, UncompressedFileSizes[k]); } return(new DataBuffer(FileBuffers[k])); }
/// <summary> /// 解压缩字节数组 /// </summary> /// <param name="data">待解压缩的字节数组</param> /// <param name="isClearData">解压缩完成后,是否清除待解压缩字节数组里面的内容</param> /// <returns>已解压的字节数组</returns> public byte[] BZipDeCompress(byte[] data, bool isClearData = true) { byte[] bytes = null; using (MemoryStream o = new MemoryStream()) { using (MemoryStream ms = new MemoryStream(data)) { using (Stream s = new BZip2InputStream(ms)) { s.Flush(); int size = 0; byte[] buffer = new byte[BUFFER_LENGTH]; while ((size = s.Read(buffer, 0, buffer.Length)) > 0) { o.Write(buffer, 0, size); } } } bytes = o.ToArray(); } if (isClearData) { Array.Clear(data, 0, data.Length); } return(bytes); }
public static byte[] UnzipBytes(byte[] compbytes) { byte[] result; MemoryStream m_msBZip2 = null; BZip2InputStream m_isBZip2 = null; try { m_msBZip2 = new MemoryStream(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 = bytesUncompressed; reader.Close(); } } finally { if (m_isBZip2 != null) { m_isBZip2.Dispose(); } if (m_msBZip2 != null) { m_msBZip2.Dispose(); } } return(result); }
public void Performance() { window_ = new WindowedStream(0x150000); outStream_ = new BZip2OutputStream(window_, 1); long target = 0x10000000; readTarget_ = writeTarget_ = target; Thread reader = new Thread(Reader); reader.Name = "Reader"; Thread writer = new Thread(Writer); writer.Name = "Writer"; DateTime startTime = DateTime.Now; writer.Start(); inStream_ = new BZip2InputStream(window_); reader.Start(); writer.Join(); reader.Join(); DateTime endTime = DateTime.Now; TimeSpan span = endTime - startTime; Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (target / 1024) / span.TotalSeconds); }
private void GetCompressedInfoReader() { StreamReader compressed_reader = null; try { Stream stream = null; if (Extension == ".gz") { stream = new GZipInputStream(Stream); } else if (Extension == ".bz2") { stream = new BZip2InputStream(Stream); } else if (Extension == ".lzma") { stream = GetLzmaStream(Stream); } compressed_reader = new StreamReader(stream); } catch (Exception e) { Log.Error(e, "Error in opening compressed man page"); if (compressed_reader != null) { compressed_reader.Close(); } Error(); return; } reader = compressed_reader; }
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]); } }
public void BZ_StreamCopy() { var src = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("Hello")); var ms = new MemoryStream(); using (var output = new BZip2OutputStream(ms, true)) { src.CopyTo(output); } Assert.IsTrue(ms.Length > 0); ms.Seek(0, SeekOrigin.Begin); using (var input = new BZip2InputStream(ms, true)) { var dst = new MemoryStream(); input.CopyTo(dst); Assert.AreEqual(dst.Length, src.Length); src.Position = 0; dst.Position = 0; while (src.Position != src.Length) { Assert.AreEqual(src.ReadByte(), dst.ReadByte()); } } }
public void ExtractGZipSample(string gzipFileName, string targetDir) { FileStream fileIn = new FileStream(gzipFileName, FileMode.Open, FileAccess.Read); string fnOut = System.IO.Path.Combine(targetDir, System.IO.Path.GetFileNameWithoutExtension(gzipFileName)); FileStream fileOut = File.Create(fnOut); try { using (BZip2InputStream bzipInput = new BZip2InputStream(fileIn)) { StreamUtils.Copy(bzipInput, fileOut, new byte[4096]); } } catch (Exception e) { //压缩不成功,就把元数据和压缩后的数据通通删掉 if (File.Exists(gzipFileName)) { File.Delete(gzipFileName); } if (File.Exists(fnOut)) { File.Delete(fnOut); } } finally { fileOut.Close(); } }
public byte[] Decompress(Stream inputStream) { using (var bzip2Stream = new BZip2InputStream(inputStream)) using (var outputStream = new MemoryStream()) { bzip2Stream.WriteTo(outputStream); return outputStream.ToArray(); } }
/// <summary> /// Decompress <paramref name="instream">input</paramref> writing /// decompressed data to <paramref name="outstream">output stream</paramref> /// </summary> public static void Decompress(Stream instream, Stream outstream) { System.IO.Stream bos = outstream; System.IO.Stream bis = instream; BZip2InputStream bzis = new BZip2InputStream(bis); int ch = bzis.ReadByte(); while (ch != -1) { bos.WriteByte((byte)ch); ch = bzis.ReadByte(); } bos.Flush(); }
private byte[] Decompress(byte[] data) { using (var input = new MemoryStream(data)) using (var output = new MemoryStream()) using (var unZip = new BZip2InputStream(input)) { int ch = unZip.ReadByte(); while (ch != -1) { output.WriteByte((byte)ch); ch = unZip.ReadByte(); } output.Flush(); return output.ToArray(); } }
public static byte[] Decompress(byte[] gz) { MemoryStream ms = new MemoryStream(); int len = BitConverter.ToInt32(gz, 0); ms.Write(gz, 4, gz.Length-4); var buffer = new byte[len]; ms.Position = 0; using (BZip2InputStream zip = new BZip2InputStream(ms)) { zip.Read(buffer, 0, buffer.Length); } return buffer; }
public override void SetBaseStream(Stream outputStream) { zipStream = new BZip2InputStream(outputStream); }
public static ThreadDatabase LoadFromFile(string filename) { Stream stream = null; ThreadDatabase db = null; DebugConsole.ShowInfo("Loading database from file: " + filename); try { IFormatter formatter = new BinaryFormatter(); stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None); if (stream.ReadByte() == (int) 'D' && stream.ReadByte() == (int) 'B' && stream.ReadByte() == (int) '2') stream = new BZip2InputStream(stream); else stream.Seek(0, SeekOrigin.Begin); long version = (long) formatter.Deserialize(stream); Debug.Assert(version == VERSION, "Unable to load database, incompatible file version.", "The specified database was created with an incompatible version of this program."); db = (ThreadDatabase) formatter.Deserialize(stream); } catch (Exception e) { DebugConsole.ShowError("Exception thrown while loading database: " + e.ToString()); } finally { if (null != stream) stream.Close(); if (null != db) { db.filename = new FileInfo(filename).FullName; db.fileHandle = new FileStream(db.filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None); // Convert path to absolute. foreach (KeyValuePair<int, Thread> kvp in db.threads) foreach (Post p in kvp.Value) if (!p.ImagePath.Contains("http:")) p.ImagePath = db.ImageDir + @"\" + p.ImagePath; } } return db; }
public static Message DecodeMessage2(Stream sr2, RSAParameters rsaprv, byte[] mypub) { Message msg = new Message(); if(false) { var ms4 = new MemoryStream(); var buf = new byte[4096]; int len2; while((len2=sr2.Read(buf, 0, buf.Length))>0){ ms4.Write(buf, 0, len2); } var data = ms4.ToArray(); System.Diagnostics.Trace.WriteLine(BitConverter.ToString(data)); System.Diagnostics.Trace.WriteLine("Pub: " + BitConverter.ToString(mypub)); sr2 = new MemoryStream(data); } var flags = sr2.ReadByte(); var compressText = (flags & 1) != 0; var SignMessage = (flags & 2) != 0; sr2.Read(msg.msgid, 0, msg.msgid.Length); sr2.Read(msg.replyTo, 0, msg.replyTo.Length); var len = sr2.ReadShort(); var szbuf = new byte[len]; sr2.Read(szbuf, 0, szbuf.Length); if (compressText == false) { msg.msg = Encoding.UTF8.GetString(szbuf); } else { var b = new Byte[1024 * 4]; using (var dec_sr = new BZip2InputStream(new MemoryStream(szbuf))) using (var ms2 = new MemoryStream()) { var readlen = 0; while ((readlen = dec_sr.Read(b, 0, b.Length)) > 0) { ms2.Write(b, 0, readlen); } ms2.Flush(); msg.msg = Encoding.UTF8.GetString(ms2.ToArray()); } } if (SignMessage) { var theirkeylen = sr2.ReadShort(); var theirpubkey = new Byte[theirkeylen]; sr2.Read(theirpubkey, 0, theirpubkey.Length); var hashlen = sr2.ReadShort(); var hash = new Byte[hashlen]; sr2.Read(hash, 0, hash.Length); //System.Diagnostics.Trace.WriteLine("Hash: " +BitConverter.ToString(hash)); using (var ms2 = new MemoryStream()) using (var rsa = new RSACryptoServiceProvider()) { ms2.Write(msg.msgid, 0, msg.msgid.Length); ms2.Write(msg.replyTo, 0, msg.replyTo.Length); var msgarr = Encoding.UTF8.GetBytes(msg.msg); ms2.WriteShort(msgarr.Length); ms2.Write(msgarr, 0, msgarr.Length); ms2.WriteShort(theirpubkey.Length); ms2.Write(theirpubkey, 0, theirpubkey.Length); ms2.WriteShort(mypub.Length); ms2.Write(mypub, 0, mypub.Length); RSAParameters rsap2; Shared.LoadKey2(Shared.pubToPem(theirpubkey), null, out rsap2); rsa.ImportParameters(rsap2); var b = rsa.VerifyData(ms2.ToArray(), SHA512OID, hash); if (b) msg.signed = true; else msg.forged = true; } msg.their_pubkey = theirpubkey; } return msg; }
/// <summary> /// 압축된 데이타를 복원한다. /// </summary> /// <param name="input">복원할 Data</param> /// <returns>복원된 Data</returns> public override byte[] Decompress(byte[] input) { if(IsDebugEnabled) log.Debug(CompressorTool.SR.DecompressStartMsg); // check input data if(input.IsZeroLength()) { if(IsDebugEnabled) log.Debug(CompressorTool.SR.InvalidInputDataMsg); return CompressorTool.EmptyBytes; } byte[] output; using(var inStream = new MemoryStream(input)) using(var bz2 = new BZip2InputStream(inStream)) using(var outStream = new MemoryStream(input.Length * 2)) { StreamTool.CopyStreamToStream(bz2, outStream, CompressorTool.BUFFER_SIZE); output = outStream.ToArray(); } if(IsDebugEnabled) log.Debug(CompressorTool.SR.DecompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length); return output; }
private bool InitializeBZ2Stream() { if (this.bz2Input != null) { return true; } try { this.bz2Input = new BZip2InputStream(this.input, true); return true; } catch (IOException) { // need to rewind the memory buffer so that the next attempt will start from // the beginning of the block this.input.Position = lastSuccessfulPosition; return false; } }
public static MemoryStream DecompressFromFile(string targetFilePath, bool isWritable) { MemoryStream ms; FileStream fs = null; try { fs = new FileStream(targetFilePath, FileMode.Open); var tmp = new byte[4]; fs.Read(tmp, 0, 4); int decompressedLength = BitConverter.ToInt32(tmp, 0); var b = new byte[decompressedLength]; using (BZip2InputStream gzs = new BZip2InputStream(fs)) //ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(fs)) { fs = null; // Read decompressed data gzs.Read(b, 0, b.Length); } ms = new MemoryStream(b, 0, b.Length, isWritable, true); } finally { if (fs!=null) fs.Dispose(); } return ms; }
/// <summary> /// Decompress <paramref name="inStream">input</paramref> writing /// decompressed data to the <paramref name="outStream">output stream</paramref> /// </summary> /// <param name="inStream">The stream containing data to decompress.</param> /// <param name="outStream">The stream to write decompressed data to.</param> /// <remarks>Both streams are closed on completion</remarks> public static void Decompress(Stream inStream, Stream outStream) { if ( inStream == null ) { throw new ArgumentNullException("inStream"); } if ( outStream == null ) { throw new ArgumentNullException("outStream"); } using ( outStream ) { using ( BZip2InputStream bzis = new BZip2InputStream(inStream) ) { int ch = bzis.ReadByte(); while (ch != -1) { outStream.WriteByte((byte)ch); ch = bzis.ReadByte(); } } } }
public DotNetZipBZip2InputStream(Stream outputStream) { zipStream = new BZip2InputStream(outputStream); }
/// <summary> /// Applies a binary patch to the data in /// <paramref name="input"/> and writes the results of patching to <paramref name="output"/>. /// </summary> /// <param name="input">A <see cref="Stream"/> containing the input data.</param> /// <param name="openPatchStream">A func that can open a <see cref="Stream"/> positioned at the start of the patch data. /// This stream must support reading and seeking, and <paramref name="openPatchStream"/> must allow multiple streams on /// the patch to be opened concurrently.</param> /// <param name="output">A <see cref="Stream"/> to which the patched data is written.</param> public static void Apply(Stream input, Func<Stream> openPatchStream, Stream output) { // check arguments if (input == null) throw new ArgumentNullException("input"); if (openPatchStream == null) throw new ArgumentNullException("openPatchStream"); if (output == null) throw new ArgumentNullException("output"); /* File format: 0 8 "BSDIFF40" 8 8 X 16 8 Y 24 8 sizeof(newfile) 32 X bzip2(control block) 32+X Y bzip2(diff block) 32+X+Y ??? bzip2(extra block) with control block a set of triples (x,y,z) meaning "add x bytes from oldfile to x bytes from the diff block; copy y bytes from the extra block; seek forwards in oldfile by z bytes". */ // read header long controlLength, diffLength, newSize; using (Stream patchStream = openPatchStream()) { // check patch stream capabilities if (!patchStream.CanRead) throw new ArgumentException("Patch stream must be readable.", "openPatchStream"); if (!patchStream.CanSeek) throw new ArgumentException("Patch stream must be seekable.", "openPatchStream"); byte[] header = ReadExactly(patchStream, c_headerSize); // check for appropriate magic long signature = ReadInt64(header, 0); if (signature != c_fileSignature) throw new InvalidOperationException("Corrupt patch."); // read lengths from header controlLength = ReadInt64(header, 8); diffLength = ReadInt64(header, 16); newSize = ReadInt64(header, 24); if (controlLength < 0 || diffLength < 0 || newSize < 0) throw new InvalidOperationException("Corrupt patch."); } // preallocate buffers for reading and writing const int c_bufferSize = 1048576; byte[] newData = new byte[c_bufferSize]; byte[] oldData = new byte[c_bufferSize]; // prepare to read three parts of the patch in parallel using (Stream compressedControlStream = openPatchStream()) using (Stream compressedDiffStream = openPatchStream()) using (Stream compressedExtraStream = openPatchStream()) { // seek to the start of each part compressedControlStream.Seek(c_headerSize, SeekOrigin.Current); compressedDiffStream.Seek(c_headerSize + controlLength, SeekOrigin.Current); compressedExtraStream.Seek(c_headerSize + controlLength + diffLength, SeekOrigin.Current); // decompress each part (to read it) using (BZip2InputStream controlStream = new BZip2InputStream(compressedControlStream)) using (BZip2InputStream diffStream = new BZip2InputStream(compressedDiffStream)) using (BZip2InputStream extraStream = new BZip2InputStream(compressedExtraStream)) { long[] control = new long[3]; byte[] buffer = new byte[8]; int oldPosition = 0; int newPosition = 0; while (newPosition < newSize) { // read control data for (int i = 0; i < 3; i++) { ReadExactly(controlStream, buffer, 0, 8); control[i] = ReadInt64(buffer, 0); } // sanity-check if (newPosition + control[0] > newSize) throw new InvalidOperationException("Corrupt patch."); // seek old file to the position that the new data is diffed against input.Position = oldPosition; int bytesToCopy = (int) control[0]; while (bytesToCopy > 0) { int actualBytesToCopy = Math.Min(bytesToCopy, c_bufferSize); // read diff string ReadExactly(diffStream, newData, 0, actualBytesToCopy); // add old data to diff string int availableInputBytes = Math.Min(actualBytesToCopy, (int) (input.Length - input.Position)); ReadExactly(input, oldData, 0, availableInputBytes); for (int index = 0; index < availableInputBytes; index++) newData[index] += oldData[index]; output.Write(newData, 0, actualBytesToCopy); // adjust counters newPosition += actualBytesToCopy; oldPosition += actualBytesToCopy; bytesToCopy -= actualBytesToCopy; } // sanity-check if (newPosition + control[1] > newSize) throw new InvalidOperationException("Corrupt patch."); // read extra string bytesToCopy = (int) control[1]; while (bytesToCopy > 0) { int actualBytesToCopy = Math.Min(bytesToCopy, c_bufferSize); ReadExactly(extraStream, newData, 0, actualBytesToCopy); output.Write(newData, 0, actualBytesToCopy); newPosition += actualBytesToCopy; bytesToCopy -= actualBytesToCopy; } // adjust position oldPosition = (int) (oldPosition + control[2]); } } } }