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]); } } }
/// <summary> /// 压缩字符串 /// </summary> /// <param name="str">需要压缩的字符串</param> /// <returns>base64位字符串</returns> public string CompressStr(string str) { //判断字符串是否为空,为空则返回结果为空 if (str == null) { return(""); } //字符串不为空则执行一下语句 try { //定义数组存放压缩字符串字节序列 byte[] bytData = System.Text.Encoding.Unicode.GetBytes(str); //创建存储内存流 MemoryStream ms = new MemoryStream(); //实例化BZip2OutputStream类 Stream s = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms); //字节压缩 s.Write(bytData, 0, bytData.Length); //关闭内存流,释放字节序列 s.Close(); //获取返回的字节序列数组 byte[] compressedData = (byte[])ms.ToArray(); //获取数组转化为base64位字符串 string result = System.Convert.ToBase64String(compressedData, 0, compressedData.Length) + " " + bytData.Length; //返回base64位字符串 return(result); } catch (Exception ex) { //报错信息 throw ex; } }
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); } }
/// <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; }
public void BZip2_Compress_Extract_Test() { var plainStream = PlainText.ToStream(); plainStream.Seek(0, SeekOrigin.Begin); var plainData = Encoding.UTF8.GetBytes(PlainText); byte[] compressedData; byte[] extractedData; // Compress using(var compressedStream = new MemoryStream()) using(var bz2 = new BZip2OutputStream(compressedStream)) { bz2.Write(plainData, 0, plainData.Length); bz2.Close(); compressedData = compressedStream.ToArray(); } Assert.IsNotNull(compressedData); // Array.Resize(ref compressedData, compressedData.Length+1); // compressedData[compressedData.Length - 1] = (byte)0; // Extract using(var compressedStream = new MemoryStream(compressedData)) using(var bz2 = new BZip2InputStream(compressedStream)) using(var extractedStream = new MemoryStream()) { StreamTool.CopyStreamToStream(bz2, extractedStream); extractedData = extractedStream.ToArray(); } Assert.IsNotNull(extractedData); string extractedText = Encoding.UTF8.GetString(extractedData).TrimEnd('\0'); Assert.AreEqual(PlainText, extractedText); }
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); } }
/// <summary> /// Compresses a byte array /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] CompressBytes(this byte[] data) { // empty if null if (data.IsNullOrEmpty()) return null; using (MemoryStream msBZip2 = new MemoryStream()) { int size = data.Length; // Prepend the compressed data with the length of the uncompressed data (firs 4 bytes) using (BinaryWriter writer = new BinaryWriter(msBZip2)) { writer.Write(size); using (BZip2OutputStream BZip2OutStream = new BZip2OutputStream(msBZip2)) { BZip2OutStream.Write(data, 0, size); } } // return the compressed data return msBZip2.ToArray(); } }
public void ConvertAll(string captureDirPath, int numEvents, IProgressFeedback progress) { List<BinaryReader> readers = new List<BinaryReader>(1); SortedList<uint, KeyValuePair<BinaryReader, uint>> ids = new SortedList<uint, KeyValuePair<BinaryReader, uint>>(numEvents); uint i = 0; foreach (string filePath in Directory.GetFiles(captureDirPath, "*.log", SearchOption.TopDirectoryOnly)) { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); readers.Add(r); while (fs.Position < fs.Length) { i++; int pct = (int)(((float)i / (float)numEvents) * 100.0f); progress.ProgressUpdate("Indexing", pct); uint id = r.ReadUInt32(); uint size = r.ReadUInt32(); ids.Add(id, new KeyValuePair<BinaryReader, uint>(r, (uint)fs.Position)); fs.Seek(size, SeekOrigin.Current); } } string resultPath = String.Format("{0}\\capture.osd", captureDirPath); BZip2OutputStream outStream = new BZip2OutputStream(new FileStream(resultPath, FileMode.Create)); XmlTextWriter xtw = new XmlTextWriter(outStream, System.Text.Encoding.UTF8); xtw.Formatting = Formatting.Indented; xtw.Indentation = 4; xtw.IndentChar = ' '; xtw.WriteStartDocument(true); xtw.WriteStartElement("events"); i = 0; foreach (KeyValuePair<BinaryReader, uint> pair in ids.Values) { i++; int pct = (int)(((float)i / (float)numEvents) * 100.0f); progress.ProgressUpdate(String.Format("Converting event {0} of {1}", i, numEvents), pct); BinaryReader r = pair.Key; uint offset = pair.Value; r.BaseStream.Seek(offset, SeekOrigin.Begin); UnserializeNode(r, xtw); } xtw.WriteEndElement(); xtw.WriteEndDocument(); xtw.Close(); foreach (BinaryReader r in readers) r.Close(); }
public void SaveToCompressedStream(System.IO.Stream aData) { using (var gzipOut = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData)) { gzipOut.IsStreamOwner = false; SaveToStream(gzipOut); gzipOut.Close(); } }
/// <summary> /// /// </summary> /// <param name="bytesToCompress"></param> /// <returns></returns> public byte[] Compress(byte[] bytesToCompress) { MemoryStream ms = new MemoryStream(); Stream s = new BZip2OutputStream(ms); //fire the contents of the byte-array into the OutputStream to perform the compression s.Write(bytesToCompress,0, bytesToCompress.Length); s.Close(); //Convert the memoryStream back into a byte Array byte[] compressedData = (byte[]) ms.ToArray(); ms.Close(); return compressedData; }
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[] Compress(byte[] input) { Contract.Requires(input != null); Contract.Ensures(Contract.Result<byte[]>() != null); var length = input.Length; var ms = new MemoryStream(length); using (var stream = new BZip2OutputStream(ms)) stream.Write(input, 0, length); return ms.ToArray(); }
/// <summary> /// 压缩 /// </summary> /// <param name="input"></param> /// <returns></returns> public static string Compress(string input) { string result = string.Empty; 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()); } }
private static Stream GenerateFrom(bool compressed, params string[] xmlEvents) { byte[] header; byte[] body; using (MemoryStream header_stream = new MemoryStream()) { using (BinaryWriter headerWriter = new BinaryWriter(header_stream, Encoding.ASCII)) { byte[] magic = Encoding.ASCII.GetBytes("oSpy"); const uint version = 2; uint isCompressed = (compressed) ? 1U : 0U; uint numEvents = (uint)xmlEvents.Length; headerWriter.Write(magic); headerWriter.Write(version); headerWriter.Write(isCompressed); headerWriter.Write(numEvents); } header = header_stream.ToArray(); } using (MemoryStream rawBodyStream = new MemoryStream()) { Stream bodyStream; if (compressed) bodyStream = new BZip2OutputStream(rawBodyStream); else bodyStream = rawBodyStream; using (BinaryWriter bodyWriter = new BinaryWriter(bodyStream, Encoding.UTF8)) { bodyWriter.Write(Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><events>")); foreach (string xml in xmlEvents) bodyWriter.Write(Encoding.UTF8.GetBytes(xml)); bodyWriter.Write(Encoding.UTF8.GetBytes("</events>")); } bodyStream.Flush(); body = rawBodyStream.ToArray(); } byte[] result = new byte[header.Length + body.Length]; header.CopyTo(result, 0); body.CopyTo(result, header.Length); return new MemoryStream(result); }
public static byte[] BZip2CompressArray(byte[] data) { MemoryStream ms = new MemoryStream(); using (BZip2OutputStream bz = new BZip2OutputStream(ms, 9)) { bz.IsStreamOwner = false; bz.Write(data, 0, data.Length); } ms.Position = 0; byte[] compressed = new byte[ms.Length]; ms.Read(compressed, 0, compressed.Length); byte[] gzBuffer = new byte[compressed.Length + 4]; System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length); System.Buffer.BlockCopy(BitConverter.GetBytes(data.Length), 0, gzBuffer, 0, 4); return gzBuffer; }
/// <summary> /// Compress the <paramref name="inStream">input stream</paramref> sending /// result data to <paramref name="outStream">output stream</paramref> /// </summary> /// <param name="inStream">The readable stream to compress.</param> /// <param name="outStream">The output stream to receive the compressed data.</param> /// <param name="isStreamOwner">Both streams are closed on completion if true.</param> /// <param name="level">Block size acts as compression level (1 to 9) with 1 giving /// the lowest compression and 9 the highest.</param> public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level) { if (inStream==null||outStream==null) { throw new Exception("Null Stream"); } try { using (var bzipOutput=new BZip2OutputStream(outStream, level)) { bzipOutput.IsStreamOwner=isStreamOwner; StreamUtils.Copy(inStream, bzipOutput, new byte[4096]); } } finally { if (isStreamOwner) { // outStream is closed by the BZip2OutputStream if stream owner inStream.Dispose(); } } }
/// <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 AddFile(string path) { string fileNameInDestination = this.GetFileNameInDestination(path); if (!Directory.Exists(Path.GetDirectoryName(fileNameInDestination))) { Directory.CreateDirectory(Path.GetDirectoryName(fileNameInDestination)); } string contents = this.GetMd5Sum(File.ReadAllBytes(path)); using (BZip2OutputStream stream = new BZip2OutputStream(new FileStream(fileNameInDestination + ".bz2", FileMode.Create, FileAccess.Write))) { byte[] buffer = File.ReadAllBytes(path); stream.Write(buffer, 0, buffer.Length); } File.WriteAllText(this.GetDestinationHashLocation(fileNameInDestination), contents); }
public static void Compress(Stream inStream, Stream outStream, int blockSize) { if ( inStream == null ) { throw new ArgumentNullException("inStream"); } if ( outStream == null ) { throw new ArgumentNullException("outStream"); } using ( inStream ) { using (BZip2OutputStream bzos = new BZip2OutputStream(outStream, blockSize)) { int ch = inStream.ReadByte(); while (ch != -1) { bzos.WriteByte((byte)ch); ch = inStream.ReadByte(); } } } }
public static string ZipString(string sBuffer) { MemoryStream m_msBZip2 = null; BZip2OutputStream m_osBZip2 = null; string 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 = Convert.ToBase64String(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> /// Creates the Transformation Block to BZip2 compress. /// </summary> /// <param name="options">The options.</param> public static TransformBlock<byte[], byte[]> Create(ExecutionDataflowBlockOptions options) { return new TransformBlock<byte[], byte[]>(data => { using (var output = new MemoryStream()) { using (Logger.BeginTimedOperation("BZip2OutputStream", null, LogEventLevel.Debug)) using (var input = new MemoryStream(data)) using (var compression = new BZip2OutputStream(output)) { input.CopyTo(compression); compression.Flush(); } var result = output.ToArray(); Logger.Debug("Compressed {InputSize} bytes to {OutputSize} ({Compression:00}%)", data.Length, result.Length, result.Length * 100M/data.Length); return result; } }, options); }
/// <summary> Saves a TrigradCompressed image to a stream. </summary> public void Save(Stream s) { using (BZip2OutputStream zipper = new BZip2OutputStream(s)) using (BinaryWriter writer = new BinaryWriter(zipper)) { writer.Write((ushort)Width); writer.Write((ushort)Height); var samples = Mesh.SelectMany(t => t.Samples).Distinct().ToList(); writer.Write((uint)samples.Count); var sorted = samples.OrderBy(kvp => kvp.Point.X * ushort.MaxValue + kvp.Point.Y).ToList(); foreach (var sample in sorted) writer.Write((ushort)sample.Point.X); foreach (var sample in sorted) writer.Write((ushort)sample.Point.Y); foreach (var sample in sorted) writer.Write(sample.Color.R); foreach (var sample in sorted) writer.Write(sample.Color.G); foreach (var sample in sorted) writer.Write(sample.Color.B); writer.Write((uint)Mesh.Count); foreach (var tri in Mesh) writer.Write(sorted.FindIndex(p => p.Point == tri.U.Point)); foreach (var tri in Mesh) writer.Write(sorted.FindIndex(p => p.Point == tri.V.Point)); foreach (var tri in Mesh) writer.Write(sorted.FindIndex(p => p.Point == tri.W.Point)); writer.Flush(); } }
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 (); }
public static void CreateArchive (ProgressMonitor mon, string folder, string targetFile) { string tf = Path.GetFileNameWithoutExtension (targetFile); if (tf.EndsWith (".tar")) tf = Path.GetFileNameWithoutExtension (tf); if (File.Exists (targetFile)) File.Delete (targetFile); using (Stream os = File.Create (targetFile)) { Stream outStream = os; // Create the zip file switch (GetArchiveExtension (targetFile)) { case ".tar.gz": outStream = new GZipOutputStream(outStream); goto case ".tar"; case ".tar.bz2": outStream = new BZip2OutputStream(outStream, 9); goto case ".tar"; case ".tar": TarArchive archive = TarArchive.CreateOutputTarArchive (outStream); archive.SetAsciiTranslation (false); archive.RootPath = folder; archive.ProgressMessageEvent += delegate (TarArchive ac, TarEntry e, string message) { if (message != null) mon.Log.WriteLine (message); }; foreach (FilePath f in GetFilesRec (new DirectoryInfo (folder))) { TarEntry entry = TarEntry.CreateEntryFromFile (f); entry.Name = f.ToRelative (folder); if (!Platform.IsWindows) { UnixFileInfo fi = new UnixFileInfo (f); entry.TarHeader.Mode = (int)fi.Protection; } else { entry.Name = entry.Name.Replace ('\\', '/'); FilePermissions p = FilePermissions.S_IFREG | FilePermissions.S_IROTH | FilePermissions.S_IRGRP | FilePermissions.S_IRUSR; if (!new FileInfo (f).IsReadOnly) p |= FilePermissions.S_IWUSR; entry.TarHeader.Mode = (int) p; } archive.WriteEntry(entry, false); } // HACK: GNU tar expects to find a double zero record at the end of the archive. TarArchive only emits one. // This hack generates the second zero block. FieldInfo tarOutField = typeof(TarArchive).GetField ("tarOut", BindingFlags.Instance | BindingFlags.NonPublic); if (tarOutField != null) { TarOutputStream tarOut = (TarOutputStream) tarOutField.GetValue (archive); tarOut.Finish (); } archive.CloseArchive (); break; case ".zip": ZipOutputStream zs = new ZipOutputStream (outStream); zs.SetLevel(5); byte[] buffer = new byte [8092]; foreach (FilePath f in GetFilesRec (new DirectoryInfo (folder))) { string name = f.ToRelative (folder); if (Platform.IsWindows) name = name.Replace ('\\', '/'); ZipEntry infoEntry = new ZipEntry (name); zs.PutNextEntry (infoEntry); using (Stream s = File.OpenRead (f)) { int nr; while ((nr = s.Read (buffer, 0, buffer.Length)) > 0) zs.Write (buffer, 0, nr); } } zs.Finish (); zs.Close (); break; default: mon.Log.WriteLine ("Unsupported file format: " + Path.GetFileName (targetFile)); return; } } }
public void SaveToCompressedStream(System.IO.Stream aData) { using (var gzipOut = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData)) { gzipOut.IsStreamOwner = false; SaveToStream(gzipOut); gzipOut.Close(); } }
/// <summary> /// Compress a given file into Tar archive. /// </summary> public void Compress() { Common.ValidateOverwrite(this.overwriteTarget, this.target); DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(this.source)); this.Log(String.Format("Archiving: [{0}] -> [{1}].", this.source, this.target), LogLevel.Minimal); if (tarLevel == TarCompressionLevel.None) { using (Stream outStream = File.Create(this.target)) { ArchiveFile(outStream, dir); } } else if (tarLevel == TarCompressionLevel.BZip2) { using (BZip2OutputStream bz2Stream = new BZip2OutputStream(File.Create(this.target), 9)) { ArchiveFile(bz2Stream, dir); } } else if (tarLevel == TarCompressionLevel.GZip) { using (GZipOutputStream gzoStream = new GZipOutputStream(File.Create(this.target))) { gzoStream.SetLevel(9); ArchiveFile(gzoStream, dir); } } this.Log(String.Format("Successfully Archived: [{0}] -> [{1}].", this.source, this.target), LogLevel.Minimal); Common.RemoveFile(this.removeSource, this.source); }
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(TimeSpan.FromMinutes(5.0D))); Assert.IsTrue(reader.Join(TimeSpan.FromMinutes(5.0D))); DateTime endTime = DateTime.Now; TimeSpan span = endTime - startTime; Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (Target / 1024) / span.TotalSeconds); }
public void TestExtractArchiveTarBz2() { CloudFilesProvider provider = (CloudFilesProvider)Bootstrapper.CreateObjectStorageProvider(); string containerName = TestContainerPrefix + Path.GetRandomFileName(); string sourceFileName = "DarkKnightRises.jpg"; byte[] content = File.ReadAllBytes("DarkKnightRises.jpg"); using (MemoryStream outputStream = new MemoryStream()) { using (BZip2OutputStream bz2Stream = new BZip2OutputStream(outputStream)) { bz2Stream.IsStreamOwner = false; using (TarArchive archive = TarArchive.CreateOutputTarArchive(bz2Stream)) { archive.IsStreamOwner = false; archive.RootPath = Path.GetDirectoryName(Path.GetFullPath(sourceFileName)).Replace('\\', '/'); TarEntry entry = TarEntry.CreateEntryFromFile(sourceFileName); archive.WriteEntry(entry, true); archive.Close(); } } outputStream.Flush(); outputStream.Position = 0; ExtractArchiveResponse response = provider.ExtractArchive(outputStream, containerName, ArchiveFormat.TarBz2); Assert.IsNotNull(response); Assert.AreEqual(1, response.CreatedFiles); Assert.IsNotNull(response.Errors); Assert.AreEqual(0, response.Errors.Count); } using (MemoryStream downloadStream = new MemoryStream()) { provider.GetObject(containerName, sourceFileName, downloadStream, verifyEtag: true); Assert.AreEqual(content.Length, GetContainerObjectSize(provider, containerName, sourceFileName)); downloadStream.Position = 0; byte[] actualData = new byte[downloadStream.Length]; downloadStream.Read(actualData, 0, actualData.Length); Assert.AreEqual(content.Length, actualData.Length); using (MD5 md5 = MD5.Create()) { byte[] contentMd5 = md5.ComputeHash(content); byte[] actualMd5 = md5.ComputeHash(actualData); Assert.AreEqual(BitConverter.ToString(contentMd5), BitConverter.ToString(actualMd5)); } } /* Cleanup */ provider.DeleteContainer(containerName, deleteObjects: true); }
/// <summary> /// This is the "real" main. The class main() instantiates a tar object /// for the application and then calls this method. Process the arguments /// and perform the requested operation. /// </summary> public void InstanceMain(string[] argv) { TarArchive archive = null; int argIdx = this.ProcessArguments(argv); if (this.archiveName != null && ! this.archiveName.Equals("-")) { if (operation == Operation.Create) { if (!Directory.Exists(Path.GetDirectoryName(archiveName))) { Console.Error.WriteLine("Directory for archive doesnt exist"); return; } } else { if (File.Exists(this.archiveName) == false) { Console.Error.WriteLine("File does not exist " + this.archiveName); return; } } } if (operation == Operation.Create) { // WRITING Stream outStream = Console.OpenStandardOutput(); if (this.archiveName != null && ! this.archiveName.Equals("-")) { outStream = File.Create(archiveName); } if (outStream != null) { switch (this.compression) { case Compression.Compress: outStream = new DeflaterOutputStream(outStream); break; case Compression.Gzip: outStream = new GZipOutputStream(outStream); break; case Compression.Bzip2: outStream = new BZip2OutputStream(outStream, 9); break; } archive = TarArchive.CreateOutputTarArchive(outStream, this.blockingFactor); } } else { // EXTRACTING OR LISTING Stream inStream = Console.OpenStandardInput(); if (this.archiveName != null && ! this.archiveName.Equals( "-" )) { inStream = File.OpenRead(archiveName); } if (inStream != null) { switch (this.compression) { case Compression.Compress: inStream = new InflaterInputStream(inStream); break; case Compression.Gzip: inStream = new GZipInputStream(inStream); break; case Compression.Bzip2: inStream = new BZip2InputStream(inStream); break; } archive = TarArchive.CreateInputTarArchive(inStream, this.blockingFactor); } } if (archive != null) { // SET ARCHIVE OPTIONS archive.SetKeepOldFiles(this.keepOldFiles); archive.AsciiTranslate = this.asciiTranslate; archive.SetUserInfo(this.userId, this.userName, this.groupId, this.groupName); } if (archive == null) { Console.Error.WriteLine( "no processing due to errors" ); } else if (operation == Operation.Create) { // WRITING if (verbose) { archive.ProgressMessageEvent += new ProgressMessageHandler(ShowTarProgressMessage); } for ( ; argIdx < argv.Length ; ++argIdx ) { string[] fileNames = GetFilesForSpec(argv[argIdx]); if (fileNames.Length > 0) { foreach (string name in fileNames) { TarEntry entry = TarEntry.CreateEntryFromFile(name); archive.WriteEntry(entry, true); } } else { Console.Error.Write("No files for " + argv[argIdx]); } } } else if (operation == Operation.List) { // LISTING archive.ProgressMessageEvent += new ProgressMessageHandler(ShowTarProgressMessage); archive.ListContents(); } else { // EXTRACTING string userDir = Environment.CurrentDirectory; if (verbose) { archive.ProgressMessageEvent += new ProgressMessageHandler(ShowTarProgressMessage); } if (userDir != null) { archive.ExtractContents(userDir); } } if (archive != null) { // CLOSE ARCHIVE archive.Close(); } }
private void saveMenuItem_Click(object sender, EventArgs e) { if (saveFileDialog.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create); BZip2OutputStream stream = new BZip2OutputStream(fs); dataSet.WriteXml(stream); stream.Close(); fs.Close(); } }
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; }
static void CreateInternal(byte[] oldData, byte[] newData, Stream output) { // check arguments if (oldData == null) throw new ArgumentNullException("oldData"); if (newData == null) throw new ArgumentNullException("newData"); if (output == null) throw new ArgumentNullException("output"); if (!output.CanSeek) throw new ArgumentException("Output stream must be seekable.", "output"); if (!output.CanWrite) throw new ArgumentException("Output stream must be writable.", "output"); /* Header is 0 8 "BSDIFF40" 8 8 length of bzip2ed ctrl block 16 8 length of bzip2ed diff block 24 8 length of new file */ /* File is 0 32 Header 32 ?? Bzip2ed ctrl block ?? ?? Bzip2ed diff block ?? ?? Bzip2ed extra block */ byte[] header = new byte[c_headerSize]; WriteInt64(c_fileSignature, header, 0); // "BSDIFF40" WriteInt64(0, header, 8); WriteInt64(0, header, 16); WriteInt64(newData.Length, header, 24); long startPosition = output.Position; output.Write(header, 0, header.Length); int[] I = SuffixSort(oldData); byte[] db = new byte[newData.Length]; byte[] eb = new byte[newData.Length]; int dblen = 0; int eblen = 0; using (WrappingStream wrappingStream = new WrappingStream(output, Ownership.None)) using (BZip2OutputStream bz2Stream = new BZip2OutputStream(wrappingStream)) { // compute the differences, writing ctrl as we go int scan = 0; int pos = 0; int len = 0; int lastscan = 0; int lastpos = 0; int lastoffset = 0; while (scan < newData.Length) { int oldscore = 0; for (int scsc = scan += len; scan < newData.Length; scan++) { len = Search(I, oldData, newData, scan, 0, oldData.Length, out pos); for (; scsc < scan + len; scsc++) { if ((scsc + lastoffset < oldData.Length) && (oldData[scsc + lastoffset] == newData[scsc])) oldscore++; } if ((len == oldscore && len != 0) || (len > oldscore + 8)) break; if ((scan + lastoffset < oldData.Length) && (oldData[scan + lastoffset] == newData[scan])) oldscore--; } if (len != oldscore || scan == newData.Length) { int s = 0; int sf = 0; int lenf = 0; for (int i = 0; (lastscan + i < scan) && (lastpos + i < oldData.Length); ) { if (oldData[lastpos + i] == newData[lastscan + i]) s++; i++; if (s * 2 - i > sf * 2 - lenf) { sf = s; lenf = i; } } int lenb = 0; if (scan < newData.Length) { s = 0; int sb = 0; for (int i = 1; (scan >= lastscan + i) && (pos >= i); i++) { if (oldData[pos - i] == newData[scan - i]) s++; if (s * 2 - i > sb * 2 - lenb) { sb = s; lenb = i; } } } if (lastscan + lenf > scan - lenb) { int overlap = (lastscan + lenf) - (scan - lenb); s = 0; int ss = 0; int lens = 0; for (int i = 0; i < overlap; i++) { if (newData[lastscan + lenf - overlap + i] == oldData[lastpos + lenf - overlap + i]) s++; if (newData[scan - lenb + i] == oldData[pos - lenb + i]) s--; if (s > ss) { ss = s; lens = i + 1; } } lenf += lens - overlap; lenb -= lens; } for (int i = 0; i < lenf; i++) db[dblen + i] = (byte) (newData[lastscan + i] - oldData[lastpos + i]); for (int i = 0; i < (scan - lenb) - (lastscan + lenf); i++) eb[eblen + i] = newData[lastscan + lenf + i]; dblen += lenf; eblen += (scan - lenb) - (lastscan + lenf); byte[] buf = new byte[8]; WriteInt64(lenf, buf, 0); bz2Stream.Write(buf, 0, 8); WriteInt64((scan - lenb) - (lastscan + lenf), buf, 0); bz2Stream.Write(buf, 0, 8); WriteInt64((pos - lenb) - (lastpos + lenf), buf, 0); bz2Stream.Write(buf, 0, 8); lastscan = scan - lenb; lastpos = pos - lenb; lastoffset = pos - scan; } } } // compute size of compressed ctrl data long controlEndPosition = output.Position; WriteInt64(controlEndPosition - startPosition - c_headerSize, header, 8); // write compressed diff data using (WrappingStream wrappingStream = new WrappingStream(output, Ownership.None)) using (BZip2OutputStream bz2Stream = new BZip2OutputStream(wrappingStream)) { bz2Stream.Write(db, 0, dblen); } // compute size of compressed diff data long diffEndPosition = output.Position; WriteInt64(diffEndPosition - controlEndPosition, header, 16); // write compressed extra data using (WrappingStream wrappingStream = new WrappingStream(output, Ownership.None)) using (BZip2OutputStream bz2Stream = new BZip2OutputStream(wrappingStream)) { bz2Stream.Write(eb, 0, eblen); } // seek to the beginning, write the header, then seek back to end long endPosition = output.Position; output.Position = startPosition; output.Write(header, 0, header.Length); output.Position = endPosition; }
private void Compress(SteppedProgressManager progress, ProgressChangedEventHandler progressChanged) { using (FileStream archiveStream = new FileStream(ReportBaseName + ".tar", FileMode.Create, FileAccess.Write)) { TarArchive archive = TarArchive.CreateOutputTarArchive(archiveStream); foreach (FileInfo file in Report.Files) { TarEntry entry = TarEntry.CreateEntryFromFile(file.FullName); entry.Name = Path.GetFileName(entry.Name); archive.WriteEntry(entry, false); } archive.Close(); } ProgressManager step = new ProgressManager(); progress.Steps.Add(new SteppedProgressManagerStep(step, 0.5f, "Compressing")); using (FileStream bzipFile = new FileStream(ReportBaseName + ".tbz", FileMode.Create)) using (FileStream tarStream = new FileStream(ReportBaseName + ".tar", FileMode.Open, FileAccess.Read, FileShare.Read, 262144, FileOptions.DeleteOnClose)) using (BZip2OutputStream bzipStream = new BZip2OutputStream(bzipFile, 262144)) { int lastRead = 0; byte[] buffer = new byte[524288]; while ((lastRead = tarStream.Read(buffer, 0, buffer.Length)) != 0) { bzipStream.Write(buffer, 0, lastRead); step.Completed = tarStream.Position; step.Total = tarStream.Length; if (progressChanged != null) progressChanged(this, new ProgressChangedEventArgs(progress, null)); } } }