本文整理汇总了C#中System.IO.Compression.DeflateStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# DeflateStream.Write方法的具体用法?C# DeflateStream.Write怎么用?C# DeflateStream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Compression.DeflateStream
的用法示例。
在下文中一共展示了DeflateStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compress
public static byte[] Compress(string data)
{
byte[] input = Encoding.ASCII.GetBytes(data);
MemoryStream ms = new MemoryStream();
DeflateStream zipper = new DeflateStream(ms, CompressionMode.Compress, true);
zipper.Write(input, 0, input.Length);
zipper.Close();
byte[] output;
if (ms.Length < input.Length)
{
output = new byte[ms.Length + 8];
SetLength(input.Length, output, 0);
SetLength((int)ms.Length, output, 4);
ms.Position = 0;
ms.Read(output, 8, output.Length - 8);
}
else
{
output = new byte[input.Length + 8];
SetLength(input.Length, output, 0);
SetLength(input.Length, output, 4);
input.CopyTo(output, 8);
}
return output;
}
示例2: Compress
public static byte[] Compress(byte[] buffer)
{
byte[] comp;
using (var output = new MemoryStream()) {
using (var deflate = new DeflateStream(output, CompressionMode.Compress))
deflate.Write(buffer, 0, buffer.Length);
comp = output.ToArray();
}
// Refer to http://www.ietf.org/rfc/rfc1950.txt for zlib format
const byte CM = 8;
const byte CINFO = 7;
const byte CMF = CM | (CINFO << 4);
const byte FLG = 0xDA;
byte[] result = new byte[comp.Length + 6];
result[0] = CMF;
result[1] = FLG;
Buffer.BlockCopy(comp, 0, result, 2, comp.Length);
uint cksum = ADLER32(buffer);
var index = result.Length - 4;
result[index++] = (byte)(cksum >> 24);
result[index++] = (byte)(cksum >> 16);
result[index++] = (byte)(cksum >> 8);
result[index++] = (byte)(cksum >> 0);
return result;
}
示例3: ZipStream
/// <summary>
/// ѹËõ¶þ½øÖÆÁ÷
/// </summary>
/// <param name="stream">´ýѹËõµÄ¶þ½øÖÆÁ÷</param>
/// <returns>·µ»ØѹËõºóµÄ¶þ½øÖÆÁ÷</returns>
/// <remarks>
/// ѹËõ¶þ½øÖÆÁ÷£ºÍ¨¹ýCreateTempFile()Éú³ÉÁÙʱÎļþ£»Éú³É´ýѹËõµÄ¶þ½øÖÆÁ÷inStream£»·µ»ØѹËõºóµÄ¶þ½øÖÆÁ÷outStream£»
/// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Compression\GenericZipTest.cs" region="StreamZipTest" lang="cs" title="ѹËõ¶þ½øÖÆÁ÷" />
/// </remarks>
public static Stream ZipStream(Stream stream)
{
if (false == PreCheck(stream))
return null;
#region compress stream by buffers
MemoryStream result = new MemoryStream();
Byte[] buff = new Byte[BufferSize];
using (DeflateStream compressStream = new DeflateStream(result, CompressionMode.Compress, true))
{
int cnt = stream.Read(buff, 0, BufferSize);
while (cnt > 0)
{
compressStream.Write(buff, 0, cnt);
cnt = stream.Read(buff, 0, BufferSize);
}
}
#endregion
return result;
}
示例4: CompressTest
public void CompressTest()
{
var s = "StartTime:13.4.201213:15:26;RunTime:00:01:24";
var ms = new MemoryStream();
var ds = new DeflateStream(ms, CompressionMode.Compress);
var encoding = System.Text.Encoding.UTF8;
var byteData = encoding.GetBytes(s);
Trace.WriteLine("original : {0}", s);
ds.Write(byteData, 0, byteData.Length);
ds.Close();
byte[] compressed = ms.ToArray();
Trace.WriteLine("compressed : {0}", System.Convert.ToBase64String(compressed));
ms = new MemoryStream(compressed);
ds = new DeflateStream(ms, CompressionMode.Decompress);
compressed = new byte[compressed.Length + 100];
var offset = 0;
while (true)
{
int bytesRead = ds.Read(compressed, offset, 1);
if (bytesRead == 0) { break; }
offset += bytesRead;
}
ds.Close();
string uncompressed = encoding.GetString(compressed);
Trace.WriteLine("uncompressed: {0}\n", uncompressed);
}
示例5: Compress
/// <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;
using(var outStream = new MemoryStream()) {
using(var deflate = new DeflateStream(outStream, CompressionMode.Compress)) {
deflate.Write(input, 0, input.Length);
}
output = outStream.ToArray();
}
if(IsDebugEnabled)
log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);
return output;
}
示例6: Compress
public void Compress(ref byte[] buffer, ref int length)
{
using (var outS = new MemoryStream())
{
using (var ds = new DeflateStream(outS, CompressionMode.Compress, true))
{
ds.Write(buffer, 0, length);
}
outS.Position = 0;
// Recycle the buffer?
if (outS.Length <= buffer.Length)
{
length = outS.Read(buffer, 0, buffer.Length);
// Heartbleed: Nope; zero-fill the remaining buffer!
for (var i = length; i < buffer.Length; i++)
{
buffer[i] = 0;
}
}
else
{
buffer = outS.ToArray();
length = buffer.Length;
}
}
}
示例7: Compress
/// <summary>
/// Returns compressed version of given string.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Compress(string str)
{
var barr = Encoding.Unicode.GetBytes(str + '\0'); // Why did we append a null byte... was this required?
using (var mout = new MemoryStream())
{
// Deflate should use optimal compression level by default (0, as defined by .NET 4.5).
using (var df = new DeflateStream(mout, CompressionMode.Compress))
{
// Write compressed data to memory stream.
df.Write(barr, 0, barr.Length);
}
// Compression method
int cmf = 8; // cm
cmf += 7 << 4; // cinfo
// Flags
int flg = 2 << 6; // Compression level
int n = ((cmf * 256) + flg) % 31;
if (n != 0)
flg += 31 - n; // Check bits
// <length>;<cmf><flg><data><checksum>
return string.Format("{0};{1:x02}{2:x02}{3}{4:x08}", barr.Length, cmf, flg, BitConverter.ToString(mout.ToArray()).Replace("-", "").ToLower(), ComputeAdler32(barr));
}
}
示例8: GetEncodedReportFile
private string GetEncodedReportFile()
{
string report = null;
try {
string exePath = Application.ExecutablePath;
string appPath = Path.GetDirectoryName(exePath);
string logFileName = Path.Combine(appPath, defaultLogFileName);
if (!File.Exists(logFileName)) {
return null;
}
byte[] logFileBytes = File.ReadAllBytes(logFileName);
using (MemoryStream ms = new MemoryStream()) {
using (DeflateStream deflateStream = new DeflateStream(ms, CompressionMode.Compress, false)) {
deflateStream.Write(logFileBytes, 0, logFileBytes.Length);
byte[] compressedBytes = ms.ToArray();
report = Convert.ToBase64String(compressedBytes);
}
}
}
catch (ArgumentException e) {
Trace.TraceWarning(e.ToString());
}
catch (IOException e) {
Trace.TraceWarning(e.ToString());
}
catch (SecurityException e) {
Trace.TraceWarning(e.ToString());
}
catch (UnauthorizedAccessException e) {
Trace.TraceWarning(e.ToString());
}
return report;
}
示例9: Compress
public byte[] Compress( byte[] buffer )
{
MemoryStream result = new MemoryStream();
int offset = 0;
BinaryWriter bw = new BinaryWriter( result );
bw.Write( new byte[] { 0x54, 0x4C, 0x5A, 0x43 } );
bw.Write( (byte)0x01 );
bw.Write( (byte)0x02 );
bw.Write( (byte)0x00 );
bw.Write( (byte)0x00 );
bw.Write( (int)0 ); // compressed size - we'll fill this in once we know it
bw.Write( (int)buffer.Length ); // decompressed size
bw.Write( (int)0 ); // unknown, 0
bw.Write( (int)0 ); // unknown, 0
using ( DeflateStream compressionStream = new DeflateStream( result, CompressionLevel.Optimal ) ) {
compressionStream.Write( buffer );
}
byte[] retval = result.ToArray();
// fill in compressed size
Util.CopyByteArrayPart( BitConverter.GetBytes( (int)retval.Length ), 0, retval, 8, 4 );
return retval;
}
示例10: Deflate
/// <summary>
/// Compresses a data buffer using a simple huffman deflate algorithem.
/// </summary>
/// <param name="data">Data buffer to compress.</param>
/// <returns>Compressed version of data buffer.</returns>
public static byte[] Deflate(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true);
deflateStream.Write(data, 0, data.Length);
deflateStream.Close();
return memoryStream.ToArray();
}
示例11: Compress
public void Compress(string content, Stream targetStream)
{
using (var writer = new DeflateStream(targetStream, CompressionMode.Compress))
{
var bytes = Encoding.UTF8.GetBytes(content);
writer.Write(bytes, 0, bytes.Length);
}
}
示例12: Compress
/// <summary>
/// Compresses the specified data.
/// </summary>
/// <param name="data">The data.</param>
/// <returns></returns>
public static byte[] Compress(byte[] data)
{
MemoryStream ms = new MemoryStream();
DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
ds.Write(data, 0, data.Length);
ds.Flush();
ds.Close();
return ms.ToArray();
}
示例13: Compress
public static byte[] Compress(byte[] raw, int offset, int count)
{
var memory = new MemoryStream();
using (var deflate = new DeflateStream(memory, CompressionMode.Compress, true))
{
deflate.Write(raw, offset, count);
return memory.ToArray();
}
}
示例14: Compress
public static byte[] Compress( byte[] input, bool addFirst2Bytes ) {
MemoryStream output = new MemoryStream();
output.Write( new byte[] { 120, 156 }, 0, 2 );
using( DeflateStream Deflate = new DeflateStream( output, CompressionMode.Compress ) ) {
Deflate.Write( input, 0, input.Length );
}
return output.ToArray();
}
示例15: CompressBlock
/// <summary>
/// 转换指定字节数组的指定区域。
/// </summary>
/// <param name="inputBuffer">要为其计算转换的输入。</param>
/// <param name="inputOffset">字节数组中的偏移量,从该位置开始使用数据。</param>
/// <param name="inputCount">字节数组中用作数据的字节数。</param>
/// <param name="outputBuffer">将转换写入的输出。</param>
/// <param name="outputOffset">输出字节数组中的偏移量,从该位置开始写入数据。</param>
/// <returns>计算所得的转换的字节数。</returns>
public override int CompressBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
using (var ms = new MemoryStream(outputBuffer, outputOffset, outputBuffer.Length - outputOffset, true)) {
using (var outStream = new DeflateStream(ms, CompressionMode.Compress)) {
outStream.Write(inputBuffer, inputOffset, inputCount);
outStream.Flush();
}
return (int)ms.Position;
}
}