本文整理汇总了C#中System.IO.Compression.GZipStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# GZipStream.Write方法的具体用法?C# GZipStream.Write怎么用?C# GZipStream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Compression.GZipStream
的用法示例。
在下文中一共展示了GZipStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompressFile
static void CompressFile(string sDir, string sRelativePath, GZipStream zipStream)
{
//Compress file name
char[] chars = sRelativePath.ToCharArray();
zipStream.Write(BitConverter.GetBytes(chars.Length), 0, sizeof(int));
foreach (char c in chars)
zipStream.Write(BitConverter.GetBytes(c), 0, sizeof(char));
//Compress file content
byte[] bytes = File.ReadAllBytes(Path.Combine(sDir, sRelativePath));
zipStream.Write(BitConverter.GetBytes(bytes.Length), 0, sizeof(int));
zipStream.Write(bytes, 0, bytes.Length);
}
示例2: Save
public static void Save(Level level, string file) {
using (Stream fs = File.Create(file),
gs = new GZipStream(fs, CompressionMode.Compress, true))
{
byte[] header = new byte[16];
BitConverter.GetBytes(1874).CopyTo(header, 0);
gs.Write(header, 0, 2);
BitConverter.GetBytes(level.Width).CopyTo(header, 0);
BitConverter.GetBytes(level.Length).CopyTo(header, 2);
BitConverter.GetBytes(level.Height).CopyTo(header, 4);
BitConverter.GetBytes(level.spawnx).CopyTo(header, 6);
BitConverter.GetBytes(level.spawnz).CopyTo(header, 8);
BitConverter.GetBytes(level.spawny).CopyTo(header, 10);
header[12] = level.rotx;
header[13] = level.roty;
header[14] = (byte)level.permissionvisit;
header[15] = (byte)level.permissionbuild;
gs.Write(header, 0, header.Length);
byte[] blocks = level.blocks;
byte[] convBlocks = new byte[blocks.Length];
for (int i = 0; i < blocks.Length; ++i) {
byte block = blocks[i];
if (block < Block.CpeCount) {
convBlocks[i] = block; //CHANGED THIS TO INCOPARATE SOME MORE SPACE THAT I NEEDED FOR THE door_orange_air ETC.
} else {
convBlocks[i] = Block.SaveConvert(block);
}
}
gs.Write(convBlocks, 0, convBlocks.Length);
// write out new blockdefinitions data
gs.WriteByte(0xBD);
int index = 0;
for (int y = 0; y < level.ChunksY; y++)
for (int z = 0; z < level.ChunksZ; z++)
for (int x = 0; x < level.ChunksX; x++)
{
byte[] chunk = level.CustomBlocks[index];
if (chunk == null) {
gs.WriteByte(0);
} else {
gs.WriteByte(1);
gs.Write(chunk, 0, chunk.Length);
}
index++;
}
}
}
示例3: Slice
static void Slice(string sourceFile, string destinationDirectory, int parts)
{
FileInfo fileInfo = new FileInfo(sourceFile);
string extension = fileInfo.Extension;
long bytesToCut = fileInfo.Length / parts;
using (FileStream fileStream = new FileStream(sourceFile, FileMode.Open))
{
byte[] buffer = new byte[bytesToCut];
for (int i = 1; i <= parts; i++)
{
using (FileStream partFileStream = new FileStream(string.Format("{0}{1}{2}{3}{4}", destinationDirectory, "Part ", i, extension, ".gz"), FileMode.Create))
{
using(GZipStream zip = new GZipStream(partFileStream, CompressionLevel.Fastest, false))
{
int lengthRead = fileStream.Read(buffer, 0, buffer.Length);
zip.Write(buffer, 0, lengthRead);
}
}
}
}
}
示例4: Run
public void Run()
{
string folder = @"c:\temp";
string uncompressedFilePath = Path.Combine(folder, "uncompressed.dat");
string compressedFilePath = Path.Combine(folder, "compressed.gz");
byte[] dataToCompress = Enumerable.Repeat((byte) 'a', 1024*1024).ToArray();
using (FileStream uncompressedFileStream = File.Create(uncompressedFilePath))
{
uncompressedFileStream.Write(dataToCompress, 0, dataToCompress.Length);
}
using (FileStream compressedFileStream = File.Create(compressedFilePath))
{
using (var compressionStream = new GZipStream(
compressedFileStream, CompressionMode.Compress))
{
compressionStream.Write(dataToCompress, 0, dataToCompress.Length);
}
}
var uncompressedFile = new FileInfo(uncompressedFilePath);
var compressedFile = new FileInfo(compressedFilePath);
Console.WriteLine(uncompressedFile.Length);
Console.WriteLine(compressedFile.Length);
}
示例5: GZCompress
public static byte[] GZCompress(byte[] source)
{
byte[] buffer;
if ((source == null) || (source.Length == 0))
{
throw new ArgumentNullException("source");
}
try
{
using (MemoryStream stream = new MemoryStream())
{
using (GZipStream stream2 = new GZipStream(stream, CompressionMode.Compress, true))
{
Console.WriteLine("Compression");
stream2.Write(source, 0, source.Length);
stream2.Flush();
stream2.Close();
Console.WriteLine("Original size: {0}, Compressed size: {1}", source.Length, stream.Length);
stream.Position = 0L;
buffer = stream.ToArray();
}
}
}
catch (Exception exception)
{
LoggingService.Error("GZip压缩时出错:", exception);
buffer = source;
}
return buffer;
}
示例6: EnCompress
/// <summary>
/// 压缩流数据
/// </summary>
/// <param name="aSourceStream"></param>
/// <returns></returns>
public static byte[] EnCompress(Stream aSourceStream)
{
MemoryStream vMemory = new MemoryStream();
aSourceStream.Seek(0, SeekOrigin.Begin);
vMemory.Seek(0, SeekOrigin.Begin);
try
{
using (GZipStream vZipStream = new GZipStream(vMemory, CompressionMode.Compress))
{
byte[] vFileByte = new byte[1024];
int vRedLen = 0;
do
{
vRedLen = aSourceStream.Read(vFileByte, 0, vFileByte.Length);
vZipStream.Write(vFileByte, 0, vRedLen);
}
while (vRedLen > 0);
}
}
finally
{
vMemory.Dispose();
}
return vMemory.ToArray();
}
示例7: Load
public override void Load()
{
// depend on HiddenFieldPageStatePersister for heavy lifting and crypto
base.Load();
CompressedSerializedData compressedData = ViewState as CompressedSerializedData;
if (compressedData == null && ControlState != null)
{
// the underlying data was not compressed
return;
}
// decompress
using (MemoryStream uncompressedStream = new MemoryStream())
{
using (GZipStream zipStream = new GZipStream(uncompressedStream, CompressionMode.Decompress, leaveOpen: true))
{
zipStream.Write(compressedData.RawData, 0, compressedData.RawData.Length);
}
uncompressedStream.Position = 0;
ObjectStateFormatter formatter = new ObjectStateFormatter();
Pair pair = (Pair)formatter.Deserialize(uncompressedStream);
// extract
ViewState = pair.First;
ControlState = pair.Second;
}
}
开发者ID:GrabYourPitchforks,项目名称:viewstate-compression,代码行数:29,代码来源:CompressedHiddenFieldPageStatePersister.cs
示例8: FileAsync_Compress_Decompress
public void FileAsync_Compress_Decompress() {
var filename = FileTool.GetTempFileName();
using(var fs = FileAsync.OpenWrite(filename))
using(var gzip = new GZipStream(fs, CompressionMode.Compress)) {
gzip.Write(PlainBytes, 0, PlainBytes.Length);
}
var fi = new FileInfo(filename);
Assert.IsTrue(fi.Exists);
Assert.IsTrue(PlainBytes.Length > fi.Length);
var outStream = new MemoryStream((int)fi.Length * 2);
using(var fs = FileAsync.OpenRead(filename))
using(var gzip = new GZipStream(fs, CompressionMode.Decompress, true)) {
var readCount = 0;
var buffer = new byte[CompressorTool.BUFFER_SIZE];
while((readCount = gzip.Read(buffer, 0, buffer.Length)) > 0) {
outStream.Write(buffer, 0, readCount);
}
var output = outStream.ToArray();
Assert.AreEqual(PlainBytes.Length, output.Length);
Assert.AreEqual(PlainBytes, output);
}
fi = new FileInfo(filename);
fi.Delete();
}
示例9: Configure
public void Configure(JsModelsConfiguration configuration)
{
// save path
Path = configuration.Path;
// compute js
var generator = new JsModelGenerator(configuration.Models);
_js = generator.GenerateModels(configuration.Models);
// minify
_js = (new Minifier()).MinifyJavaScript(_js);
// get version hash
var encoding = new UTF8Encoding();
var bytes = encoding.GetBytes(_js);
VersionHash = Convert.ToBase64String(SHA512.Create().ComputeHash(bytes));
// compress and read out to byte array
using (var ms = new MemoryStream())
{
using (var stream = new GZipStream(ms, CompressionLevel.Optimal, false))
{
stream.Write(bytes, 0, bytes.Length);
}
_jsCompressed = ms.ToArray();
}
}
示例10: Compress
public static byte[] Compress(this string value)
{
//Transform string into byte[]
var byteArray = new byte[value.Length];
var index = 0;
foreach (char item in value)
{
byteArray[index++] = (byte)item;
}
//Prepare for compress
using (var ms = new MemoryStream())
{
using (var sw = new GZipStream(ms, CompressionMode.Compress))
{
//Compress
sw.Write(byteArray, 0, byteArray.Length);
//Close, DO NOT FLUSH cause bytes will go missing...
}
//Transform byte[] zip data to string
byteArray = ms.ToArray();
}
return byteArray;
}
示例11: CompressFile
/// <summary>
/// GZip Compresses a file at the given file path.
/// </summary>
/// <param name="filepath">The path to the file to compress with gzip.</param>
public static void CompressFile(string filepath)
{
if (!File.Exists(filepath))
return;
const int chunkSize = 65536;
try {
using (var fs = new FileStream(filepath, FileMode.Open)) {
using (var gs = new GZipStream(new FileStream("Temp.gz", FileMode.Create), CompressionMode.Compress)) {
var buffer = new byte[chunkSize];
while (true) {
var bytesRead = fs.Read(buffer, 0, chunkSize);
if (bytesRead == 0) break;
gs.Write(buffer, 0, bytesRead);
}
}
}
File.Delete(filepath);
File.Move("Temp.gz", filepath);
} catch {
GC.Collect();
}
}
示例12: Compress
/// <inheritdoc />
public byte[] Compress(byte[] data, string dataName)
{
var output = new MemoryStream();
using (var stream = new GZipStream(output, CompressionLevel.Optimal))
stream.Write(data, 0, data.Length);
return output.ToArray();
}
示例13: WriteGzip
public static void WriteGzip(XmlDocument theDoc, Stream theStream)
{
MemoryStream ms = new MemoryStream();
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Encoding = Encoding.UTF8;
xmlSettings.ConformanceLevel = ConformanceLevel.Document;
xmlSettings.Indent = false;
xmlSettings.NewLineOnAttributes = false;
xmlSettings.CheckCharacters = true;
xmlSettings.IndentChars = "";
XmlWriter tw = XmlWriter.Create(ms, xmlSettings);
theDoc.WriteTo(tw);
tw.Flush();
tw.Close();
byte[] buffer = ms.GetBuffer();
GZipStream compressedzipStream = new GZipStream(theStream, CompressionMode.Compress, true);
compressedzipStream.Write(buffer, 0, buffer.Length);
// Close the stream.
compressedzipStream.Flush();
compressedzipStream.Close();
// Force a flush
theStream.Flush();
}
示例14: Compress
/// <summary>
/// 对byte数组进行压缩
/// </summary>
/// <param name="data">待压缩的byte数组</param>
/// <returns>压缩后的byte数组</returns>
public static byte[] Compress(byte[] data)
{
try
{
byte[] buffer = null;
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(data, 0, data.Length);
zip.Close();
}
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
ms.Close();
}
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
示例15: FileCompress
public void FileCompress(string sourceFile, string destinationFile)
{
// ----- Decompress a previously compressed string.
// First, create the input file stream.
FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
// ----- Create the output file stream.
FileStream destinationStream = new FileStream(destinationFile, FileMode.Create, FileAccess.Write);
// ----- Bytes will be processed by a compression
// stream.
GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true);
// ----- Process bytes from one file into the other.
const int BlockSize = 4096;
byte[] buffer = new byte[BlockSize + 1];
int bytesRead = default(int);
do
{
bytesRead = sourceStream.Read(buffer, 0, BlockSize);
if (bytesRead == 0)
{
break;
}
compressedStream.Write(buffer, 0, bytesRead);
} while (true);
// ----- Close all the streams.
sourceStream.Close();
compressedStream.Close();
destinationStream.Close();
}