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); using (BZip2InputStream inStream = new BZip2InputStream(ms)) { byte[] buf2 = new byte[buf.Length]; int pos = 0; while (true) { int numRead = inStream.Read(buf2, pos, 4096); if (numRead <= 0) { break; } pos += numRead; } for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } } }
public void 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]); } }
/// <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; } }
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> /// 지정된 데이타를 압축한다. /// </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 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 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); }
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(); }
/// <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()); } }
/// <summary> /// Compress <paramref name="instream">input stream</paramref> sending /// result to <paramref name="outputstream">output stream</paramref> /// </summary> public static void Compress(Stream instream, Stream outstream, int blockSize) { var bos = outstream; var bis = instream; var ch = bis.ReadByte(); var bzos = new BZip2OutputStream(bos, blockSize); while (ch != -1) { bzos.WriteByte((byte)ch); ch = bis.ReadByte(); } bis.Close(); bzos.Close(); }
public static void Compress(Stream instream, Stream outstream, int blockSize) { Stream stream = outstream; Stream stream2 = instream; int num = stream2.ReadByte(); BZip2OutputStream stream3 = new BZip2OutputStream(stream, blockSize); while (num != -1) { stream3.WriteByte((byte)num); num = stream2.ReadByte(); } stream2.Close(); stream3.Close(); }
/// <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())); }
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; }
override public void AddToStream (Stream stream, EventTracker tracker) { if (ChildCount > 1) throw new Exception ("Bzip2 file " + Uri + " has " + ChildCount + " children"); if (tracker != null) tracker.ExpectingAdded (UriFu.UriToEscapedString (this.Uri)); UnclosableStream unclosable; unclosable = new UnclosableStream (stream); BZip2OutputStream bz2_out; bz2_out = new BZip2OutputStream (unclosable); MemoryStream memory; memory = new MemoryStream (); // There should just be one child foreach (FileObject file in Children) file.AddToStream (memory, tracker); bz2_out.Write (memory.ToArray (), 0, (int) memory.Length); memory.Close (); bz2_out.Close (); }
/// <summary> /// 压缩字符串(ICSharpCode.SharpZipLib版) /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static string CompressByteToStr(byte[] buffer) { using (MemoryStream outputStream = new MemoryStream()) { using (BZip2OutputStream zipStream = new BZip2OutputStream(outputStream)) { zipStream.Write(buffer, 0, buffer.Length); zipStream.Close(); } return Convert.ToBase64String(outputStream.ToArray()); } }
/// <summary> /// Creates the tar file. /// </summary> protected override void ExecuteTask() { TarArchive archive = null; Stream outstream = null; Log(Level.Info, "Tarring {0} files to '{1}'.", TarFileSets.FileCount, DestFile.FullName); try { if (!LongPathDirectory.Exists(DestFile.DirectoryName)) { LongPathDirectory.Create(DestFile.DirectoryName); } outstream = File.Create(DestFile.FullName); // wrap outputstream with corresponding compression method switch (CompressionMethod) { case TarCompressionMethod.GZip: outstream = new GZipOutputStream(outstream); break; case TarCompressionMethod.BZip2: outstream = new BZip2OutputStream(outstream); break; } // create tar archive archive = TarArchive.CreateOutputTarArchive(outstream, TarBuffer.DefaultBlockFactor); // do not use convert line endings of text files to \n, as this // converts all content to ASCII archive.AsciiTranslate = false; // process all filesets foreach (TarFileSet fileset in TarFileSets) { string basePath = fileset.BaseDirectory.FullName; if (Path.GetPathRoot(basePath) != basePath) { basePath = Path.GetDirectoryName(basePath + Path.DirectorySeparatorChar); } // add files to tar foreach (string file in fileset.FileNames) { // ensure file exists (in case "asis" was used) if (!File.Exists(file)) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "File '{0}' does not exist.", file), Location); } // the filename of the tar entry string entryFileName; // the directory of the tar entry string entryDirName = string.Empty; // determine name of the tar entry if (!Flatten && file.StartsWith(basePath)) { entryFileName = file.Substring(basePath.Length); if (entryFileName.Length > 0 && entryFileName[0] == Path.DirectorySeparatorChar) { entryFileName = entryFileName.Substring(1); } // get directory part of entry entryDirName = Path.GetDirectoryName(entryFileName); // ensure directory separators are understood on linux if (Path.DirectorySeparatorChar == '\\') { entryDirName = entryDirName.Replace(@"\", "/"); } // get filename part of entry entryFileName = Path.GetFileName(entryFileName); } else { entryFileName = Path.GetFileName(file); } // add prefix if specified if (fileset.Prefix != null) { entryDirName = fileset.Prefix + entryDirName; } // ensure directory has trailing slash if (entryDirName.Length != 0) { if (!entryDirName.EndsWith("/")) { entryDirName += '/'; } // create directory entry in archive CreateDirectoryEntry(archive, entryDirName, fileset); } TarEntry entry = TarEntry.CreateEntryFromFile(file); entry.Name = entryDirName + entryFileName; entry.GroupId = fileset.Gid; entry.GroupName = fileset.GroupName; entry.UserId = fileset.Uid; entry.UserName = fileset.UserName; entry.TarHeader.Mode = fileset.FileMode; // write file to tar file archive.WriteEntry(entry, true); } // add (possibly empty) directories to zip if (IncludeEmptyDirs) { // add (possibly empty) directories to tar foreach (string directory in fileset.DirectoryNames) { // skip directories that are not located beneath the base // directory if (!directory.StartsWith(basePath) || directory.Length <= basePath.Length) { continue; } // determine tar entry name string entryName = directory.Substring(basePath.Length + 1); // add prefix if specified if (fileset.Prefix != null) { entryName = fileset.Prefix + entryName; } // ensure directory separators are understood on linux if (Path.DirectorySeparatorChar == '\\') { entryName = entryName.Replace(@"\", "/"); } if (!entryName.EndsWith("/")) { // trailing directory signals to #ziplib that we're // dealing with directory entry entryName += "/"; } // create directory entry in archive CreateDirectoryEntry(archive, entryName, fileset); } } } // close the tar archive archive.Close(); } catch (Exception ex) { // close the tar output stream if (outstream != null) { outstream.Close(); } // close the tar archive if (archive != null) { archive.Close(); } // delete the (possibly corrupt) tar file if (DestFile.Exists) { DestFile.Delete(); } throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Tar file '{0}' could not be created.", DestFile.FullName), Location, ex); } }
public void SaveToCompressedStream(System.IO.Stream aData) { using (var gzipOut = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData)) { gzipOut.IsStreamOwner = false; SaveToStream(gzipOut); gzipOut.Close(); } }
public void CompressAndSendBuffer(ref byte []buffer,DataType bDataType,ref int nX ,ref int nY) { if(buffer != null) { try { MemoryStream msCompressed = new MemoryStream(); BZip2OutputStream zosCompressed = new BZip2OutputStream(msCompressed); zosCompressed.Write(buffer, 0, buffer.Length); zosCompressed.Close(); buffer =msCompressed.ToArray(); } catch(Exception ex) { WebMeeting.Client.ClientUI.getInstance().ShowExceptionMessage("Module ::: AppShare public void CompressAndSendBuffer(ref byte []buffer,DataType bDataType,ref int nX ,ref int nY)",ex,"",false); } } try { OnDataAvailable(ref buffer,bDataType,ref nX,ref nY); } catch(Exception ex) { WebMeeting.Client.ClientUI.getInstance().ShowExceptionMessage("Module ::: AppShare public void CompressAndSendBuffer(ref byte []buffer,DataType bDataType,ref int nX ,ref int nY)",ex,"",false); } }
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; }
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(); } }