public void Start() { MatchData = new Match(); using (var compressedStream = _matchInformation != null ? Helpers.GetStreamFromUrl(_matchInformation.roundstats.map) : replayStream) using (var outputStream = new MemoryStream()) { if (replayCompressed) { BZip2.Decompress(compressedStream, outputStream, false); outputStream.Seek(0L, SeekOrigin.Begin); parser = new DemoParser(outputStream); } else { parser = new DemoParser(compressedStream); } parser.MatchStarted += MatchStarted_Event; parser.ParseHeader(); parser.ParseToEnd(); MatchEnded_Event(); } }
public static int Main(string[] args) { if (args.Length == 0) { ShowHelp(); return(1); } var parser = new ArgumentParser(args); switch (parser.Command) { case Command.Help: ShowHelp(); break; case Command.Compress: Console.WriteLine("Compressing {0} to {1} at level {2}", parser.Source, parser.Target, parser.Level); BZip2.Compress(File.OpenRead(parser.Source), File.Create(parser.Target), true, parser.Level); break; case Command.Decompress: Console.WriteLine("Decompressing {0} to {1}", parser.Source, parser.Target); BZip2.Decompress(File.OpenRead(parser.Source), File.Create(parser.Target), true); break; } return(0); }
/// <summary> /// 解压spk /// <see href="https://musoucrow.github.io/2017/07/21/spk_analysis/"/> /// </summary> /// <param name="stream"></param> /// <returns></returns> public static byte[] Decompress(Stream stream) { stream.Seek(272); stream.ReadToEnd(out byte[] content); var parts = content.Split(HEADER); using (var ms = new MemoryStream()) { for (var i = 1; i < parts.Length; i++) { var list = parts[i].Split(MARK); var data = HEADER.Concat(list[0]); using (var ts = new MemoryStream(data)) { BZip2.Decompress(ts, ms, false); } if (list.Length > 1) { for (var j = 1; j < list.Length - 1; j++) { ms.Write(list[j].Sub(32)); } var last = list.Last(); var pos = last.LastIndexOf(TAIL); if (pos > -1) { ms.Write(last.Sub(32, pos + 1)); } } } return(ms.ToArray()); } }
public CacheArchive(JagexBuffer buffer) { var decompressedSize = buffer.ReadTriByte(); var compressedSize = buffer.ReadTriByte(); if (decompressedSize != compressedSize) { byte[] tmp = new byte[buffer.Capacity() - 6]; buffer.ReadBytes(tmp, 0, buffer.Capacity() - 6); byte[] compressed = ReconstructHeader(new DefaultJagexBuffer(tmp)); MemoryStream outs = new MemoryStream(); BZip2.Decompress(new MemoryStream(compressed), outs, true); buffer = new DefaultJagexBuffer(outs.ToArray()); extractedAsWhole = true; } var size = buffer.ReadUShort(); InitializeFiles(size); var position = buffer.Position() + (size * DescriptorSize); for (var i = 0; i < size; i++) { fileHashes[i] = buffer.ReadInt(); unpackedSizes[i] = buffer.ReadTriByte(); packedSizes[i] = buffer.ReadTriByte(); positions[i] = position; position += packedSizes[i]; } this.buffer = buffer; }
public static void SaveLocal(ProjectList allProjects, /* Filter filter, */ string output, string basename, bool compressed = true, bool uncompressed = true) { // var json_string = allProjects.ToFilteredJson(filter); var json_string = allProjects.ToPrettyJson(); // var currentFile = Path.Combine(cache, "current.json"); var completeFile = Path.Combine(output, $"{basename}.json"); var compressedFile = Path.Combine(output, $"{basename}.json.bz2"); //uncompressed if (uncompressed) { File.WriteAllText(completeFile, json_string); } // File.WriteAllText(currentFile, json_string); //compressed if (compressed) { using (var fileOutStream = File.OpenWrite(compressedFile)) { byte[] byteArray = Encoding.ASCII.GetBytes(json_string); MemoryStream stream = new MemoryStream(byteArray); BZip2.Compress(stream, fileOutStream, true, 4096); } } }
public static int BZip2Decompress(byte[] input, int start, int length, byte[] output) { using var inputStream = new MemoryStream(input, start, length); using var outputStream = new MemoryStream(output, true); BZip2.Decompress(inputStream, outputStream, false); return((int)outputStream.Position); }
/// <summary> /// Create BZip2 File from given JSon /// </summary> /// <param name="targetFile"></param> /// <param name="jsonStr"></param> /// <param name="overrideFile"></param> private static void CreateBZip2File(string targetFile, string jsonStr, bool overrideFile = false) { if (overrideFile || !File.Exists(targetFile)) { Console.WriteLine($"Creating: {targetFile}"); var uncompressedData = Encoding.UTF8.GetBytes(jsonStr); byte[] inputBytes = uncompressedData; byte[] targetByteArray; using (MemoryStream sourceStream = new MemoryStream(inputBytes)) { using (MemoryStream targetStream = new MemoryStream()) { BZip2.Compress(sourceStream, targetStream, true, 4096); targetByteArray = targetStream.ToArray(); var file = File.Create(targetFile); file.Write(targetByteArray, 0, targetByteArray.Length); file.Flush(); file.Close(); } } } else { Console.WriteLine($"File.Exists.Skipping: {targetFile}"); } }
private static byte[] BZip2Decompress(Stream Data, int ExpectedLength) { MemoryStream output = new MemoryStream(); BZip2.Decompress(Data, output); return(output.ToArray()); }
/// <summary> /// Downloads a file from the specified <paramref name="url"/>, /// storing in <paramref name="path"/>, under name <paramref name="uncompressedFileName"/>. /// </summary> /// /// <param name="url">The URL where the file should be downloaded from.</param> /// <param name="path">The path where the file will be stored localy.</param> /// <param name="uncompressedFileName">The generated name of the uncompressed file.</param> /// /// <returns><c>true</c> if the download succeeded, <c>false</c> otherwise.</returns> /// public static bool Download(string url, string path, out string uncompressedFileName) { string name = System.IO.Path.GetFileName(url); string downloadedFileName = System.IO.Path.Combine(path, name); if (!File.Exists(downloadedFileName)) { Directory.CreateDirectory(path); using (var client = new WebClient()) client.DownloadFile(url, downloadedFileName); } // If the file is compressed, decompress it to disk if (downloadedFileName.EndsWith(".bz2", StringComparison.InvariantCultureIgnoreCase)) { uncompressedFileName = downloadedFileName.Remove(downloadedFileName.Length - 4); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFileName, FileMode.Open)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew)) { BZip2.Decompress(compressedFile, uncompressedFile, false); } } } else if (downloadedFileName.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase)) { uncompressedFileName = downloadedFileName.Remove(downloadedFileName.Length - 3); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFileName, FileMode.Open)) using (var decompressedFile = new GZipInputStream(compressedFile)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew)) { decompressedFile.CopyTo(uncompressedFile); } } } else if (downloadedFileName.EndsWith(".Z", StringComparison.InvariantCultureIgnoreCase)) { uncompressedFileName = downloadedFileName.Remove(downloadedFileName.Length - 2); if (!File.Exists(uncompressedFileName)) { using (var compressedFile = new FileStream(downloadedFileName, FileMode.Open)) using (var decompressedFile = new Accord.IO.Compression.LzwInputStream(compressedFile)) using (var uncompressedFile = new FileStream(uncompressedFileName, FileMode.CreateNew)) { decompressedFile.CopyTo(uncompressedFile); } } } else { uncompressedFileName = downloadedFileName; } return(true); }
// This loads the snapshot from harddisk into memory internal void RestoreFromFile() { lock (this) { if (isdisposed) { return; } if (!isondisk) { return; } isondisk = false; // Read the file data MemoryStream instream = new MemoryStream(File.ReadAllBytes(filename)); // Decompress data MemoryStream outstream = new MemoryStream((int)instream.Length * 4); instream.Seek(0, SeekOrigin.Begin); BZip2.Decompress(instream, outstream); recstream = new MemoryStream(outstream.ToArray()); // Clean up instream.Dispose(); File.Delete(filename); filename = null; } }
public IEnumerable <MessageData> Unchunk(int[]?allowedTopicIds = null) { using var inStream = new MemoryStream(Data !); var stream = new MemoryStream(); if (Compression == CompressionType.Bz2) { BZip2.Decompress(inStream, stream, false); } else { stream = inStream; } stream.Position = 0; var res = new List <MessageData>(50); while (stream.Position < stream.Length) { var message = RecordsFactory.Read <MessageData>(stream, MessageData.OpCode); if (message != null && (allowedTopicIds == null || allowedTopicIds.Contains(message.ConnectionId))) { res.Add(message); } } stream.Dispose(); return(res); }
// This moves the snapshot from memory to harddisk internal void WriteToFile() { lock (this) { if (isdisposed) { return; } if (isondisk) { return; } isondisk = true; // Compress data recstream.Seek(0, SeekOrigin.Begin); MemoryStream outstream = new MemoryStream((int)recstream.Length); BZip2.Compress(recstream, outstream, 300000); // Make temporary file filename = General.MakeTempFilename(General.Map.TempPath, "snapshot"); // Write data to file File.WriteAllBytes(filename, outstream.ToArray()); // Remove data from memory recstream.Dispose(); recstream = null; outstream.Dispose(); } }
public void Decompress(MapModel map) { var tempFile = Path.Combine(_tempFolder, map.DownloadableFileName); int tries = 0; bool success = false; do { try { FileInfo zipFileName = new FileInfo(tempFile); using (FileStream fileToDecompressAsStream = zipFileName.OpenRead()) { string decompressedFileName = Path.Combine(_tempFolder, map.LocalFileName); using (FileStream decompressedStream = File.Create(decompressedFileName)) { BZip2.Decompress(fileToDecompressAsStream, decompressedStream, true); success = true; break; } } } catch (Exception ex) { Console.WriteLine("Can't decompress " + map); } } while (tries < 5); if (!success) { throw new Exception("Can't decompress " + map); } }
// Token: 0x06000003 RID: 3 RVA: 0x000020C0 File Offset: 0x000002C0 public static byte[] Decompress(Stream stream) { stream.Seek(272, SeekOrigin.Begin); byte[] data; StreamExtensions.ReadToEnd(stream, out data); byte[][] array = data.Split(Spks.HEADER); byte[] result; using (MemoryStream memoryStream = new MemoryStream()) { for (int i = 1; i < array.Length; i++) { byte[][] array2 = array[i].Split(Spks.MARK); using (MemoryStream memoryStream2 = new MemoryStream(Arrays.Concat <byte>(Spks.HEADER, array2[0]))) { BZip2.Decompress(memoryStream2, memoryStream, false); } if (array2.Length > 1) { for (int j = 1; j < array2.Length - 1; j++) { StreamExtensions.Write(memoryStream, array2[j].Sub(32)); } byte[] array3 = array2.Last <byte[]>(); int num = array3.LastIndexof(Spks.TAIL); num = ((num < 0) ? (num + array3.Length) : num); StreamExtensions.Write(memoryStream, array3.Sub(32, num)); } } result = memoryStream.ToArray(); } return(result); }
private CompressionResult GetBZipResult(string source, Stopwatch stopwatch) { stopwatch.Restart(); var inputBytes = Encoding.UTF8.GetBytes(source); using (var sourceStream = new MemoryStream(inputBytes)) using (var targetStream = new MemoryStream()) { BZip2.Compress(sourceStream, targetStream, true, 9); var result = Convert.ToBase64String(targetStream.ToArray()); var data = WebUtility.UrlEncode(result); stopwatch.Stop(); return new CompressionResult { Name = "BZip", Data = data, PercentOfSource = 100 - (((decimal)data.Length / source.Length) * 100), Time = stopwatch.ElapsedMilliseconds }; } }
private static byte[] BZip2Decompress(Stream data, int expectedLength) { MemoryStream output = new MemoryStream(expectedLength); BZip2.Decompress(data, output, true); return(output.ToArray()); }
public static void LoadPackages(string cydiaRepos) { WebClient webClient = new WebClient(); try { webClient.DownloadFile(string.Concat(cydiaRepos, "/Packages.bz2"), "Packages.bz2"); FileStream fileStream = (new FileInfo("Packages.bz2")).OpenRead(); using (fileStream) { FileStream fileStream1 = File.Create("Packages"); using (fileStream1) { BZip2.Decompress(fileStream, fileStream1, true); } } } catch (Exception exception1) { ProjectData.SetProjectError(exception1); try { webClient.DownloadFile(string.Concat(cydiaRepos, "/Packages"), "Packages"); } catch (Exception exception) { ProjectData.SetProjectError(exception); Interaction.MsgBox("Invalid cydia", MsgBoxStyle.OkOnly, null); ProjectData.ClearProjectError(); } ProjectData.ClearProjectError(); } }
private byte[] CompressData(CompressionType compressionType, byte[] data) { if (compressionType == CompressionType.None) { return(data); } if (compressionType == CompressionType.Bzip2) { using var outputStream = new MemoryStream(); BZip2.Compress(new MemoryStream(data), outputStream, true, 1); // Remove BZh1 (note that 1 is the block size/compression level). return(outputStream.ToArray().Skip(4).ToArray()); } if (compressionType == CompressionType.Gzip) { using var outputStream = new MemoryStream(); GZip.Compress(new MemoryStream(data), outputStream, true, 512, 9); return(outputStream.ToArray()); } if (compressionType == CompressionType.Lzma) { throw new NotImplementedException("LZMA compression is currently not implemented."); } throw new EncodeException($"Unknown compression type {compressionType}."); }
public static byte[] BZip2Decompress(byte[] input, int start, int length) { using var inputStream = new MemoryStream(input, start, length); using var outputStream = new MemoryStream(); BZip2.Decompress(inputStream, outputStream, false); return(outputStream.ToArray()); }
//使用BZIP解压<--单个-->文件的方法 public static bool UnBzipFile(string zipfilename, string unzipfilename) { bool blResult;//表示解压是否成功的返回结果 //为压缩文件创建文件流实例,作为解压方法的输入流参数 using (FileStream zipFile = File.OpenRead(zipfilename)) { //为目标文件创建文件流实例,作为解压方法的输出流参数 using (FileStream destFile = File.Open(unzipfilename, FileMode.Create)) { try { BZip2.Decompress(zipFile, destFile, true);//解压文件 blResult = true; } catch (Exception ee) { Console.WriteLine(ee.Message); blResult = false; } destFile.Close(); //关闭目标文件流 zipFile.Close(); //关闭压缩文件流 return(blResult); } } }
public byte[] GetFile(int hash) { for (var i = 0; i < fileHashes.Length; i++) { if (fileHashes[i] == hash) { if (!extractedAsWhole) { var compressed = new byte[packedSizes[i]]; Buffer.BlockCopy(buffer.Array(), positions[i], compressed, 0, compressed.Length); compressed = ReconstructHeader(compressed); var outs = new MemoryStream(); BZip2.Decompress(new MemoryStream(compressed), outs, true); return(outs.ToArray()); } var decompressed = new byte[unpackedSizes[i]]; Buffer.BlockCopy(buffer.Array(), positions[i], decompressed, 0, decompressed.Length); return(decompressed); } } throw new FileNotFoundException(); }
//使用BZIP压缩<--单个-->文件的方法 public static bool BZipFile(string sourcefilename, string zipfilename) { bool blResult;//表示压缩是否成功的返回结果 //为源文件创建文件流实例,作为压缩方法的输入流参数 using (FileStream srcFile = File.OpenRead(sourcefilename)) { //为压缩文件创建文件流实例,作为压缩方法的输出流参数 using (FileStream zipFile = File.Open(zipfilename, FileMode.Create)) { try { //以4096字节作为一个块的方式压缩文件 BZip2.Compress(srcFile, zipFile, true, 9); blResult = true; } catch (Exception ee) { Console.WriteLine(ee.Message); blResult = false; } srcFile.Close(); //关闭源文件流 zipFile.Close(); //关闭压缩文件流 return(blResult); } } }
public static async Task GetBZip2FileAsync(string url, string file) { var sourceFile = DependencyService.Get <ISystem>().GetLocalFilePath("temp"); File.Delete(sourceFile); var cli = new WebClient(); await cli.DownloadFileTaskAsync(url, sourceFile); using (FileStream fileToDecompressAsStream = File.OpenRead(sourceFile)) { using (FileStream decompressedStream = File.Create(DependencyService.Get <ISystem>().GetLocalFilePath(file))) { try { BZip2.Decompress(fileToDecompressAsStream, decompressedStream, true); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } File.Delete(sourceFile); }
public GeoKDBushTests() { string geoDataDir = Directory.GetCurrentDirectory() + "\\Data"; string jsonCities = geoDataDir + "\\cities.json"; string bz2Cities = geoDataDir + "\\cities.bz2"; if (!File.Exists(jsonCities)) { if (!File.Exists(bz2Cities)) { throw new InvalidOperationException("No test data found, either as zip or json."); } BZip2.Decompress(File.OpenRead(bz2Cities), File.Create(jsonCities), true); } using (var file = File.OpenText(jsonCities)) { var serializer = new JsonSerializer(); cities = (List <City>)serializer.Deserialize(file, typeof(List <City>)); } index = new KDBush <City>(cities.ToArray(), p => p.Lon, p => p.Lat, nodeSize: 10); geoKdBush = new GeoKDBush <City>(); }
public void Compress(string target) { FileInfo fileToBeZipped = new FileInfo(target); FileInfo zipFileName = new FileInfo(string.Concat(fileToBeZipped.FullName, ".bz2")); if (System.IO.File.Exists(zipFileName.FullName)) { System.IO.File.Delete(zipFileName.FullName); } using (FileStream fileToBeZippedAsStream = fileToBeZipped.OpenRead()) { using (FileStream zipTargetAsStream = zipFileName.Create()) { try { BZip2.Compress(fileToBeZippedAsStream, zipTargetAsStream, true, 9); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
private void button_buildPackages_Click(object sender, EventArgs e) { Packages = GetPackages(); Directory.CreateDirectory(tmpPath); using (StreamWriter writer = new StreamWriter(File.Create(tmpPath + @"\Packages"))) { foreach (List <string> package in Packages) { foreach (string line in package) { writer.WriteLine(line); } writer.WriteLine(""); } } using (FileStream packagesFile = new FileInfo(tmpPath + @"\Packages").OpenRead()) { using (FileStream bzfile = File.Create(repoPath + @"\Packages.bz2")) { try { BZip2.Compress(packagesFile, bzfile, true, 4096); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } Directory.GetFiles(tmpPath).ToList().ForEach(x => File.Delete(x)); Directory.Delete(tmpPath, true); }
public void recursiveCompress(DirectoryInfo target) { compressingState = "Compressing: " + target.FullName; var files = target.GetFilesByExtensions(allowedExtensions); foreach (FileInfo file in files) { if (cancelCompress) { return; } using (var inFs = file.OpenRead()) { using (var outFs = File.Create(file.FullName + ".bz2")) { BZip2.Compress(inFs, outFs, true, compressLevel); } } file.Delete(); } var dirs = target.GetDirectories(); foreach (var dir in dirs) { if (cancelCompress) { return; } recursiveCompress(dir); } }
private static bool BzipFile(string Path) { if (!File.Exists(Path)) { return(false); } FileInfo fileToBeZipped = new FileInfo(Path); FileInfo zipFileName = new FileInfo(string.Concat(fileToBeZipped.FullName, ".bz2")); using (FileStream fileToBeZippedAsStream = fileToBeZipped.OpenRead()) { using (FileStream zipTargetAsStream = zipFileName.Create()) { try { BZip2.Compress(fileToBeZippedAsStream, zipTargetAsStream, true, 4096); File.Delete(Path); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } } } return(true); }
/// <summary> /// Unpacks a plugin package. /// </summary> /// <param name="path">The path to the package</param> /// <returns>The path to the unpacked DLL file of the plugin</returns> private static string Unpack(string path) { try { string suffix = Path.GetRandomFileName(); suffix = "_" + suffix.Replace(".", ""); string bz2File = path; string tarFile = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + ".tar"); string dest = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + suffix); U.L(LogLevel.Debug, "PLUGIN", "Decompressing " + path); BZip2.Decompress(File.OpenRead(bz2File), File.Create(tarFile), true); Stream inStream = File.OpenRead(tarFile); TarArchive archive = TarArchive.CreateInputTarArchive(inStream, TarBuffer.DefaultBlockFactor); Directory.CreateDirectory(dest); archive.ExtractContents(dest); archive.Close(); inStream.Close(); File.Delete(tarFile); return(Path.Combine(dest, Path.GetFileNameWithoutExtension(path) + ".dll")); } catch (Exception e) { U.L(LogLevel.Error, "PLUGIN", "Could not unpack plugin package: " + e.Message); return(null); } }
static void Main() { Console.Title = "Auto Bzip Compressing Path: [" + worker + "]"; Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("Scanning..." + Environment.NewLine); var fail = 0; var succ = 0; var logs = new List <string>(); var todo = new List <string>(); Directory.GetFileSystemEntries(worker, "*.*", SearchOption.AllDirectories) .OrderBy(p => p).ToList().ForEach(f => { var fa = File.GetAttributes(f); if (fa.HasFlag(FileAttributes.Directory) || f.Contains(myself) || worker.Equals(Path.GetDirectoryName(f)) || ".bz2".Equals(Path.GetExtension(f))) { return; } todo.Add(f); }); Directory.CreateDirectory(Path.Combine(worker, "bzip")); todo.ForEach(f => { try { var t = Path.Combine(worker, "bzip", f.RelativePath() + ".bz2"); Directory.CreateDirectory(Path.GetDirectoryName(t)); var i = new FileStream(f, FileMode.Open); var o = new FileStream(t, FileMode.Create); BZip2.Compress(i, o, true, 9); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Compressed => {0}", i.Name.RelativePath()); succ++; } catch (Exception e) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Compression failed => {0} => {1}", f.RelativePath(), e.Message); logs.Add(string.Format("Compression failed => {0} => {1}", f.RelativePath(), e.Message)); fail++; } finally { Console.Title = "Auto Bzip Compressing Path: [" + worker + "]" + " " + succ + "/" + fail + "/" + todo.Count; } }); var list = Path.Combine(worker, "autobz2.log"); File.WriteAllLines(list, logs.ToArray(), new UTF8Encoding(false)); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine(Environment.NewLine); Console.WriteLine("Total: {0} files | Success: {1} | Failure: {2}", todo.Count, succ, fail); Console.ReadKey(true); }