本文整理汇总了C#中ICSharpCode.SharpZipLib.GZip.GZipOutputStream.Finish方法的典型用法代码示例。如果您正苦于以下问题:C# GZipOutputStream.Finish方法的具体用法?C# GZipOutputStream.Finish怎么用?C# GZipOutputStream.Finish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.GZip.GZipOutputStream
的用法示例。
在下文中一共展示了GZipOutputStream.Finish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GZip_Compress_Extract_Test
public void GZip_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 gzs = new GZipOutputStream(compressedStream)) {
gzs.SetLevel(5);
gzs.Write(plainData, 0, plainData.Length);
gzs.Finish();
compressedData = compressedStream.ToArray();
}
Assert.IsNotNull(compressedData);
// Extract
using(var compressedStream = new MemoryStream(compressedData)) {
// compressedStream.Seek(0, SeekOrigin.Begin);
using(var gzs = new GZipInputStream(compressedStream))
using(var extractedStream = new MemoryStream()) {
StreamTool.CopyStreamToStream(gzs, extractedStream);
extractedData = extractedStream.ToArray();
}
}
Assert.IsNotNull(extractedData);
string extractedText = Encoding.UTF8.GetString(extractedData).TrimEnd('\0');
Assert.AreEqual(PlainText, extractedText);
}
示例2: TestGZip
public void TestGZip()
{
MemoryStream ms = new MemoryStream();
GZipOutputStream outStream = new GZipOutputStream(ms);
byte[] buf = new byte[100000];
System.Random rnd = new Random();
rnd.NextBytes(buf);
outStream.Write(buf, 0, buf.Length);
outStream.Flush();
outStream.Finish();
ms.Seek(0, SeekOrigin.Begin);
GZipInputStream inStream = new GZipInputStream(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]);
}
}
示例3: GetDataSet
public void GetDataSet()
{
SoapContext sc = HttpSoapContext.ResponseContext;
if (null == sc)
{
throw new ApplicationException("Only SOAP requests allowed");
}
SqlConnection conn = new SqlConnection(@"data source=(local)\NetSDK;" +
"initial catalog=Northwind;integrated security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM customers", conn);
DataSet ds = new DataSet("CustomerDS");
da.Fill(ds, "Customers");
// dispose of all objects that are no longer necessary
da.Dispose();
conn.Dispose();
MemoryStream memoryStream = new MemoryStream(1024);
GZipOutputStream gzipStream = new GZipOutputStream(memoryStream);
ds.WriteXml(gzipStream);
gzipStream.Finish();
memoryStream.Seek(0, SeekOrigin.Begin);
DimeAttachment dimeAttachment = new DimeAttachment("application/x-gzip",
TypeFormatEnum.MediaType,
memoryStream);
sc.Attachments.Add(dimeAttachment);
}
示例4: 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 compressedStream = new MemoryStream(input.Length)) {
using(var gzs = new GZipOutputStream(compressedStream)) {
gzs.SetLevel(ZipLevel);
gzs.Write(input, 0, input.Length);
gzs.Finish();
}
output = compressedStream.ToArray();
}
if(IsDebugEnabled)
log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);
return output;
}
示例5: TestGZip
public void TestGZip()
{
MemoryStream ms = new MemoryStream();
GZipOutputStream outStream = new GZipOutputStream(ms);
byte[] buf = new byte[100000];
System.Random rnd = new Random();
rnd.NextBytes(buf);
outStream.Write(buf, 0, buf.Length);
outStream.Flush();
outStream.Finish();
ms.Seek(0, SeekOrigin.Begin);
GZipInputStream inStream = new GZipInputStream(ms);
byte[] buf2 = new byte[buf.Length];
int currentIndex = 0;
int count = buf2.Length;
while (true) {
int numRead = inStream.Read(buf2, currentIndex, count);
if (numRead <= 0) {
break;
}
currentIndex += numRead;
count -= numRead;
}
Assert.AreEqual(0, count);
for (int i = 0; i < buf.Length; ++i) {
Assert.AreEqual(buf2[i], buf[i]);
}
}
示例6: DoubleClose
public void DoubleClose()
{
var memStream = new TrackedMemoryStream();
var s = new GZipOutputStream(memStream);
s.Finish();
s.Close();
s.Close();
memStream = new TrackedMemoryStream();
using (GZipOutputStream no2 = new GZipOutputStream(memStream)) {
s.Close();
}
}
示例7: Compress
private const int COMPRESS_LEVEL = 7;// 0-9, 9 being the highest compression
#endregion
#region ICompressionProvider Members
public byte[] Compress(byte[] data)
{
using (var outputStream = new MemoryStream())
{
using (var compressStream = new GZipOutputStream(outputStream))
{
compressStream.SetLevel(COMPRESS_LEVEL);
compressStream.Write(data, 0, data.Length);
compressStream.Finish();
compressStream.Close();
return outputStream.ToArray();
}
}
}
示例8: WriteAfterFinish
public void WriteAfterFinish()
{
TrackedMemoryStream memStream=new TrackedMemoryStream();
GZipOutputStream s=new GZipOutputStream(memStream);
s.Finish();
try
{
s.WriteByte(7);
Assert.Fail("Write should fail");
}
catch
{
}
}
示例9: DoubleFooter
public void DoubleFooter()
{
TrackedMemoryStream memStream=new TrackedMemoryStream();
GZipOutputStream s=new GZipOutputStream(memStream);
s.Finish();
Int64 length=memStream.Length;
s.Close();
Assert.AreEqual(length, memStream.ToArray().Length);
}
示例10: TrailingGarbage
public void TrailingGarbage()
{
/* ARRANGE */
var ms = new MemoryStream();
var outStream = new GZipOutputStream(ms);
// input buffer to be compressed
byte[] buf = new byte[100000];
var rnd = new Random();
rnd.NextBytes(buf);
// compress input buffer
outStream.Write(buf, 0, buf.Length);
outStream.Flush();
outStream.Finish();
// generate random trailing garbage and add to the compressed stream
byte[] garbage = new byte[4096];
rnd.NextBytes(garbage);
ms.Write(garbage, 0, garbage.Length);
// rewind the concatenated stream
ms.Seek(0, SeekOrigin.Begin);
/* ACT */
// decompress concatenated stream
var inStream = new GZipInputStream(ms);
byte[] buf2 = new byte[buf.Length];
int currentIndex = 0;
int count = buf2.Length;
while (true) {
int numRead = inStream.Read(buf2, currentIndex, count);
if (numRead <= 0) {
break;
}
currentIndex += numRead;
count -= numRead;
}
/* ASSERT */
Assert.AreEqual(0, count);
for (int i = 0; i < buf.Length; ++i) {
Assert.AreEqual(buf2[i], buf[i]);
}
}
示例11: GzipCompressDecompress
public void GzipCompressDecompress()
{
for (int i = 100; i < 10000;i++ )
{
var str = new string('B', i) + "a";
var outputStream = new MemoryStream();
var zoutStream = new GZipOutputStream(outputStream);
var buf = Encoding.UTF8.GetBytes(str);
zoutStream.Write(buf, 0, buf.Length);
zoutStream.Flush();
zoutStream.Finish();
outputStream.Seek(0, SeekOrigin.Begin);
//var compressed = new StreamReader(ms).ReadToEnd();
//ms.Seek(0, SeekOrigin.Begin);
var inStream = new GZipInputStream(outputStream);
var buf2 = new byte[buf.Length];
int currentIndex = 0;
int count = buf2.Length;
while (true)
{
int numRead = inStream.Read(buf2, currentIndex, count);
if (numRead <= 0)
{
break;
}
currentIndex += numRead;
count -= numRead;
}
Assert.AreEqual(0, count);
for (var j = 0; j < buf.Length; ++j)
{
Assert.AreEqual(buf2[j], buf[j]);
}
}
}
示例12: Set
//.........这里部分代码省略.........
{
// if we fail to serialize, then
// we bail
if(log.IsErrorEnabled)
{
log.Error(GetLocalizedString("set failed to serialize").Replace("$$Object$$", obj.ToString()), e);
}
return false;
}
}
else
{
val = new byte[0];
length = 0;
}
}
// now try to compress if we want to
// and if the length is over the threshold
if(_compressEnable && length > _compressThreshold)
{
if(log.IsInfoEnabled)
{
log.Info(GetLocalizedString("set trying to compress data"));
log.Info(GetLocalizedString("set size prior").Replace("$$Size$$", length.ToString(new NumberFormatInfo())));
}
try
{
MemoryStream memoryStream = new MemoryStream();
GZipOutputStream gos = new GZipOutputStream(memoryStream);
gos.Write(val, 0, length);
gos.Finish();
// store it and set compression flag
val = memoryStream.GetBuffer();
length = (int)memoryStream.Length;
flags |= F_COMPRESSED;
if(log.IsInfoEnabled)
{
log.Info(GetLocalizedString("set compression success").Replace("$$Size$$", length.ToString(new NumberFormatInfo())));
}
}
catch(IOException e)
{
if(log.IsErrorEnabled)
{
log.Error(GetLocalizedString("set compression failure"), e);
}
}
}
// now write the data to the cache server
try
{
OperationResult opResult = new OperationResult();
switch (cmdname)
{
case "set":
opResult = memcachedProvider.Set(key, (uint)flags, GetExpirationTime(expiry), val);
break;
case "add":
opResult = memcachedProvider.Add(key, (uint)flags, GetExpirationTime(expiry), val);
break;
示例13: Serialize
/// <summary>
/// Serializes a message to the GenuineChunkedStream stream.
/// </summary>
/// <param name="stream">Stream to serialize the message to.</param>
/// <param name="message">The message being serialized.</param>
/// <param name="compress">Indicates whether content should be compressed.</param>
public static void Serialize(GenuineChunkedStream stream, Message message, bool compress)
{
// write a mark whether the content will be compressed
BinaryWriter binaryWriter = new BinaryWriter(stream);
binaryWriter.Write((bool) compress);
// gather messages into a separate stream if compression is required
GenuineChunkedStream usedStream = stream;
if (compress)
usedStream = new GenuineChunkedStream(false);
binaryWriter = new BinaryWriter(usedStream);
// all simple values
binaryWriter.Write((byte) message.GenuineMessageType);
binaryWriter.Write(message.MessageId);
binaryWriter.Write(message.ReplyToId);
// binaryWriter.Write(message.Recipient.Uri);
binaryWriter.Write((bool) message.IsOneWay);
// set DestinationMarshalByRef if it is specified in headers
object broadcastObjRefOrCourt = message.ITransportHeaders[Message.TransportHeadersBroadcastObjRefOrCourt];
if (broadcastObjRefOrCourt != null)
message.DestinationMarshalByRef = broadcastObjRefOrCourt;
// objref if it exists
if (message.DestinationMarshalByRef != null)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryWriter.Write( true );
binaryFormatter.Serialize(binaryWriter.BaseStream, message.DestinationMarshalByRef);
}
else
binaryWriter.Write( false );
// Security Session parameters
binaryWriter.Write((Int16) message.SecuritySessionParameters.Attributes);
binaryWriter.Write(message.SecuritySessionParameters.RemoteTransportUser);
// now headers
foreach (DictionaryEntry entry in message.ITransportHeaders)
{
string key = entry.Key as string;
if (key == null || key == "__" || key == Message.TransportHeadersBroadcastObjRefOrCourt
|| entry.Value == null || key == Message.TransportHeadersGenuineMessageType)
continue;
string val = entry.Value.ToString();
// now write these strings
binaryWriter.Write(key);
binaryWriter.Write(val);
}
// headers end tag
binaryWriter.Write("__");
// and finally the content
usedStream.WriteStream(message.Stream);
// compress the content
if (compress)
{
GZipOutputStream compressingStream = new GZipOutputStream(new NonClosableStream(stream));
GenuineUtility.CopyStreamToStream(usedStream, compressingStream);
compressingStream.Finish();
}
}
示例14: flush
public void flush()
{
byte[] uncompressed_bytes = uncompressed_output.ToArray();
// write uncompressed size to output
new BinaryWriter(output).Write((UInt32)uncompressed_bytes.Length);
// write the compressed data
GZipOutputStream zipstream = new GZipOutputStream(this.output);
zipstream.SetLevel(1); // 0 is no compression, 9 is best compression (slowest)
zipstream.Write(uncompressed_bytes, 0, uncompressed_bytes.Length);
zipstream.Finish();
}
示例15: Set
//.........这里部分代码省略.........
if(log.IsErrorEnabled)
{
log.Error(GetLocalizedString("set failed to serialize").Replace("$$Object$$", obj.ToString()), e);
}
// return socket to pool and bail
sock.Close();
sock = null;
return false;
}
}
else
{
val = new byte[0];
length = 0;
}
}
// now try to compress if we want to
// and if the length is over the threshold
if(_compressEnable && length > _compressThreshold)
{
if(log.IsInfoEnabled)
{
log.Info(GetLocalizedString("set trying to compress data"));
log.Info(GetLocalizedString("set size prior").Replace("$$Size$$", length.ToString(new NumberFormatInfo())));
}
try
{
MemoryStream memoryStream = new MemoryStream();
GZipOutputStream gos = new GZipOutputStream(memoryStream);
gos.Write(val, 0, length);
gos.Finish();
// store it and set compression flag
val = memoryStream.GetBuffer();
length = (int)memoryStream.Length;
flags |= F_COMPRESSED;
if(log.IsInfoEnabled)
{
log.Info(GetLocalizedString("set compression success").Replace("$$Size$$", length.ToString(new NumberFormatInfo())));
}
}
catch(IOException e)
{
if(log.IsErrorEnabled)
{
log.Error(GetLocalizedString("set compression failure"), e);
}
}
}
// now write the data to the cache server
try
{
string cmd = cmdname + " " + key + " " + flags + " "
+ GetExpirationTime(expiry) + " " + length + "\r\n";
sock.Write(UTF8Encoding.UTF8.GetBytes(cmd));
sock.Write(val,0,length);
sock.Write(UTF8Encoding.UTF8.GetBytes("\r\n"));
sock.Flush();
// get result code
string line = sock.ReadLine();