本文整理汇总了C#中GZipStream.Close方法的典型用法代码示例。如果您正苦于以下问题:C# GZipStream.Close方法的具体用法?C# GZipStream.Close怎么用?C# GZipStream.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GZipStream
的用法示例。
在下文中一共展示了GZipStream.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compress
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Compress(byte[] data)
{
using (var compressedStream = new MemoryStream())
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
{
zipStream.Write(data, 0, data.Length);
zipStream.Close();
return compressedStream.ToArray();
}
}
示例2: Main
static void Main(string[] args)
{
if (args.Length < 1)
{
usage();
return;
}
else
{
string inputFile = args[0];
string outputFile = inputFile + ".gz";
try
{
// Get bytes from input stream
FileStream inFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, inputFile), FileMode.Open);
byte[] buffer = new byte[inFileStream.Length];
inFileStream.Read(buffer, 0, buffer.Length);
inFileStream.Close();
// Create GZip file stream and compress input bytes
FileStream outFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, outputFile), FileMode.Create);
GZipStream compressedStream = new GZipStream(outFileStream, CompressionMode.Compress);
compressedStream.Write(buffer, 0, buffer.Length);
compressedStream.Close();
outFileStream.Close();
Console.WriteLine("The file has been compressed. UR Da Bomb!!!");
}
catch (FileNotFoundException)
{
Console.WriteLine("Error: Specified file cannot be found.");
}
}
}
示例3: readStream
public static MemoryStream readStream(FileStream stream)
{
MemoryStream outStream = new MemoryStream();
GZipStream compress = new GZipStream(stream, CompressionMode.Decompress, false);
byte[] buffer = new Byte[stream.Length];
while (true)
{
int count = compress.Read(buffer, 0, buffer.Length);
if (count != 0)
{
outStream.Write(buffer, 0, buffer.Length);
}
if (count != buffer.Length)
{
break;
}
}
compress.Close();
outStream.Close();
stream.Close();
return new MemoryStream(outStream.ToArray());
}
示例4: Compress
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
示例5: TestInit
public void TestInit()
{
Stream baseStream = new MemoryStream();
GZipStream gzipStream = new GZipStream(baseStream, CompressionMode.Decompress);
Assert.AreSame(baseStream, gzipStream.BaseStream);
gzipStream.Close();
}
示例6: Compress
public static byte[] Compress(this byte[] bytes)
{
using (MemoryStream ms = new MemoryStream()) {
GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);
Compress.Write(bytes, 0, bytes.Length);
Compress.Close();
return ms.ToArray();
}
}
示例7: Decompress
public static byte[] Decompress(this Byte[] bytes)
{
using (MemoryStream tempMs = new MemoryStream()) {
using (MemoryStream ms = new MemoryStream(bytes)) {
GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress);
Decompress.CopyTo(tempMs);
Decompress.Close();
return tempMs.ToArray();
}
}
}
示例8: TestDecompress
public void TestDecompress()
{
Stream baseStream = new MemoryStream(this.compressedData);
GZipStream gzipStream = new GZipStream(baseStream, CompressionMode.Decompress);
StreamReader reader = new StreamReader(gzipStream);
string data = reader.ReadToEnd();
Assert.AreEqual(Data, data);
gzipStream.Close();
}
示例9: CompressGZip
/// <summary>
/// A string extension method that compress the given string to GZip byte array.
/// </summary>
/// <param name="stringToCompress">The stringToCompress to act on.</param>
/// <returns>The string compressed into a GZip byte array.</returns>
/// <example>
/// <code>
/// using System;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
/// using Z.ExtensionMethods;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_String_CompressGZip
/// {
/// [TestMethod]
/// public void CompressGZip()
/// {
/// // Type
/// string @this = "FizzBuzz";
///
/// // Exemples
/// var result = @this.CompressGZip();
///
/// // Unit Test
/// Assert.AreEqual("FizzBuzz", result.DecompressGZip());
/// }
/// }
/// }
/// </code>
/// </example>
public static byte[] CompressGZip(this string stringToCompress)
{
byte[] stringAsBytes = Encoding.Default.GetBytes(stringToCompress);
using (var memoryStream = new MemoryStream())
{
using (var zipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
zipStream.Write(stringAsBytes, 0, stringAsBytes.Length);
zipStream.Close();
return (memoryStream.ToArray());
}
}
}
示例10: saveStream
public static void saveStream(Stream stream, String location)
{
FileStream outStream = File.Create(location);
GZipStream compress = new GZipStream(outStream, CompressionMode.Compress, false);
byte[] buffer = new Byte[stream.Length];
int read = stream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
compress.Write(buffer, 0, read);
read = stream.Read(buffer, 0, buffer.Length);
}
compress.Close();
outStream.Close();
stream.Close();
}
示例11: Decompress
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input,
CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
示例12: CompressFile
public static void CompressFile(string toCompressFileName,string targetFileName,bool IsDeleteSourceFile)
{
FileStream reader;
reader = File.Open(toCompressFileName, FileMode.Open);
FileStream writer;
writer = File.Create(targetFileName);
//压缩相关的流
MemoryStream ms = new MemoryStream();
GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress, true);
//往压缩流中写数据
byte[] sourceBuffer = new byte[reader.Length];
reader.Read(sourceBuffer, 0, sourceBuffer.Length);
zipStream.Write(sourceBuffer, 0, sourceBuffer.Length);
//一定要在内存流读取之前关闭压缩流
zipStream.Close();
zipStream.Dispose();
//从内存流中读数据
ms.Position = 0; //注意,不要遗漏此句
byte[] destBuffer = new byte[ms.Length];
ms.Read(destBuffer, 0, destBuffer.Length);
writer.Write(destBuffer, 0, destBuffer.Length);
//关闭并释放内存流
ms.Close();
ms.Dispose();
//关闭并释放文件流
writer.Close();
writer.Dispose();
reader.Close();
reader.Dispose();
if (IsDeleteSourceFile)
{
File.Delete(toCompressFileName);
}
}
示例13: Gzip
/// <summary>
/// Compacta um array de bytes com Zip
/// <summary>
//public static byte[] Zip(byte[] inputByteArray)
//{
// if (inputByteArray == null || inputByteArray.Length == 0)
// {
// return new byte[0];
// }
// try
// {
// using (MemoryStream ms = new MemoryStream())
// {
// using (ZipOutputStream zipOut = new ZipOutputStream(ms))
// {
// ZipEntry ZipEntry = new ZipEntry("ZippedFile");
// zipOut.PutNextEntry(ZipEntry);
// zipOut.SetLevel(9);
// zipOut.Write(inputByteArray, 0, inputByteArray.Length);
// zipOut.Finish();
// zipOut.Close();
// return ms.ToArray();
// }
// }
// }
// catch
// {
// return new byte[0];
// }
//}
/// <summary>
/// Descompacta um array de bytes com Zip
/// <summary>
//public byte[] UnZip(byte[] inputByteArray)
//{
// if (inputByteArray == null || inputByteArray.Length == 0)
// {
// return new byte[0];
// }
// try
// {
// using (MemoryStream ms = new MemoryStream(inputByteArray))
// {
// using (MemoryStream ret = new MemoryStream())
// {
// using (ZipInputStream zipIn = new ZipInputStream(ms))
// {
// ZipEntry theEntry = zipIn.GetNextEntry();
// byte[] buffer = new byte[2048];
// int size = 2048;
// while (true)
// {
// size = zipIn.Read(buffer, 0, buffer.Length);
// if (size > 0)
// {
// ret.Write(buffer, 0, size);
// }
// else
// {
// break;
// }
// }
// return ret.ToArray();
// }
// }
// }
// }
// catch
// {
// return new byte[0];
// }
//}
/// <summary>
/// Compacta um array de bytes com GZip
/// <summary>
public byte[] Gzip(byte[] data)
{
if (data == null || data.Length == 0)
{
return new byte[0];
}
try
{
using (MemoryStream output = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(output, CompressionMode.Compress))
{
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
}
}
catch
{
return new byte[0];
}
}
示例14: SetDatabaseVersion
} // End of the GetSqlString method
#endregion
#region Database versioning
/// <summary>
/// Save the database version to an xml file
/// </summary>
/// <param name="databaseVersion">The database version to update to</param>
/// <returns>A error message</returns>
public static void SetDatabaseVersion(Int32 databaseVersion)
{
// Set the filename
string filename = HttpContext.Current.Server.MapPath("/DatabaseFiles/DatabaseVersion.xml.gz");
// Create variables
GZipStream gzipStream = null;
XmlTextWriter xmlTextWriter = null;
try
{
// Create a gzip stream
gzipStream = new GZipStream(new FileStream(filename, FileMode.Create), CompressionMode.Compress);
// Create a xml text writer
xmlTextWriter = new XmlTextWriter(gzipStream, new UTF8Encoding(true));
// Write the start tag of the document
xmlTextWriter.WriteStartDocument();
// Write the root element
xmlTextWriter.WriteStartElement("Database");
// Write information to the document
CreateXmlRow(xmlTextWriter, "Version", databaseVersion.ToString());
// Write the end tag for the xml document
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
}
catch (Exception e)
{
throw e;
}
finally
{
// Close streams
if (xmlTextWriter != null)
{
// Close the XmlTextWriter
xmlTextWriter.Close();
}
if (gzipStream != null)
{
// Close the gzip stream
gzipStream.Close();
}
}
} // End of the SetDatabaseVersion method
示例15: Zip
/// <summary>
/// Zips the file
/// </summary>
public void Zip()
{
byte[] byteArray = null;
byteArray = GetRawFile(Url);
//Prepare for compress
using (MemoryStream ms = new MemoryStream())
using (GZipStream sw = new GZipStream(ms, CompressionMode.Compress))
{
//Compress
sw.Write(byteArray, 0, byteArray.Length);
//Close, DO NOT FLUSH cause bytes will go missing...
sw.Close();
byteArray = ms.ToArray();
ByteArrayToFile(string.Format("{0}{1}", BaseServerPath, FileName), byteArray);
}
FileLink = VirtualPathUtility.ToAbsolute(System.Configuration.ConfigurationManager.AppSettings["TEMPPATH"]) + FileName;
}