本文整理汇总了C#中DeflateStream类的典型用法代码示例。如果您正苦于以下问题:C# DeflateStream类的具体用法?C# DeflateStream怎么用?C# DeflateStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DeflateStream类属于命名空间,在下文中一共展示了DeflateStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessNativeResources
void ProcessNativeResources(bool compress)
{
var unprocessedNameMatch = new Regex(@"^(.*\.)?costura(32|64)\.", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
var processedNameMatch = new Regex(@"^costura(32|64)\.", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
foreach (var resource in ModuleDefinition.Resources.OfType<EmbeddedResource>())
{
var match = unprocessedNameMatch.Match(resource.Name);
if (match.Success)
{
resource.Name = resource.Name.Substring(match.Groups[1].Length).ToLowerInvariant();
hasUnmanaged = true;
}
if (processedNameMatch.IsMatch(resource.Name))
{
using (var stream = resource.GetResourceStream())
{
if (compress && resource.Name.EndsWith(".zip"))
{
using (var compressStream = new DeflateStream(stream, CompressionMode.Decompress))
{
checksums.Add(resource.Name, CalculateChecksum(compressStream));
}
}
else
{
checksums.Add(resource.Name, CalculateChecksum(stream));
}
}
}
}
}
示例2: DecompressProfile
static void DecompressProfile(string srcfile, string dstfile)
{
try
{
Console.WriteLine("Reading source...");
byte[] x = File.ReadAllBytes(srcfile); // load all bytes
byte[] y = new byte[x.Length-0x1A];
Console.WriteLine("Copying array...");
Array.Copy(x, 0x1A, y, 0, y.Length); // skip hash (0x14) + uncompresed size (0x4) + zlib compression type (0x2) = 0x1A
Console.WriteLine("Initializing streams...");
DeflateStream defStream = new DeflateStream(new MemoryStream(y),CompressionMode.Decompress);
FileStream fs = File.Create(dstfile);
Console.WriteLine("Decompressing data to destination...");
defStream.CopyTo(fs);
Console.WriteLine("Decompression done.\n" + dstfile);
defStream.Close();
fs.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.GetType().Name + " | " + ex.Message);
Console.Beep();
}
return;
}
示例3: OverlappingFlushAsync_DuringFlushAsync
public static async Task OverlappingFlushAsync_DuringFlushAsync()
{
byte[] buffer = null;
string testFilePath = gzTestFile("GZTestDocument.pdf");
using (var origStream = await LocalMemoryStream.readAppFileAsync(testFilePath))
{
buffer = origStream.ToArray();
}
using (var writeStream = new ManualSyncMemoryStream(false))
using (var zip = new DeflateStream(writeStream, CompressionMode.Compress))
{
Task task = null;
try
{
writeStream.manualResetEvent.Set();
await zip.WriteAsync(buffer, 0, buffer.Length);
writeStream.manualResetEvent.Reset();
writeStream.WriteHit = false;
task = zip.FlushAsync();
Assert.True(writeStream.WriteHit);
Assert.Throws<InvalidOperationException>(() => { zip.FlushAsync(); }); // "overlapping flushes"
}
finally
{
// Unblock Async operations
writeStream.manualResetEvent.Set();
// The original WriteAsync should be able to complete
Assert.True(task.Wait(100 * 500));
}
}
}
示例4: CompressResponse
/// <summary>
/// Compresses the response stream if the browser allows it.
/// </summary>
private static MemoryStream CompressResponse(Stream responseStream, HttpApplication app, string key)
{
MemoryStream dataStream = new MemoryStream();
StreamCopy(responseStream, dataStream);
responseStream.Dispose();
byte[] buffer = dataStream.ToArray();
dataStream.Dispose();
MemoryStream ms = new MemoryStream();
Stream compress = null;
if (IsEncodingAccepted(DEFLATE))
{
compress = new DeflateStream(ms, CompressionMode.Compress);
app.Application.Add(key + "enc", DEFLATE);
}
else if (IsEncodingAccepted(GZIP))
{
compress = new GZipStream(ms, CompressionMode.Compress);
app.Application.Add(key + "enc", DEFLATE);
}
compress.Write(buffer, 0, buffer.Length);
compress.Dispose();
return ms;
}
示例5: Compress
public static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and already compressed files.
if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".cmp")
{
// Create the compressed file.
using (FileStream outFile =
File.Create(fi.FullName + ".cmp"))
{
using (DeflateStream Compress =
new DeflateStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into
// the compression stream.
inFile.CopyTo(Compress);
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}
}
示例6: Decompress
public static void Decompress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Get original file extension,
// for example "doc" from report.doc.cmp.
string curFile = fi.FullName;
string origName = curFile.Remove(curFile.Length
- fi.Extension.Length);
//Create the decompressed file.
using (FileStream outFile = File.Create(origName))
{
using (DeflateStream Decompress = new DeflateStream(inFile,
CompressionMode.Decompress))
{
// Copy the decompression stream
// into the output file.
Decompress.CopyTo(outFile);
Console.WriteLine("Decompressed: {0}", fi.Name);
}
}
}
}
示例7: Decompress
public static byte[] Decompress(byte[] data)
{
try
{
string result = string.Empty;
byte[] buffer = { };
MemoryStream ms = new MemoryStream(data);
Stream s = new DeflateStream(ms, CompressionMode.Decompress);
int len = 4096;
while (true)
{
int oldLen = buffer.Length;
Array.Resize(ref buffer, oldLen + len);
int size = s.Read(buffer, oldLen, len);
if (size != len)
{
Array.Resize(ref buffer, buffer.Length - (len - size));
break;
}
if (size <= 0)
break;
}
s.Close();
return buffer;
}
catch
{
return null;
}
}
示例8: Compress
// netz.compress.ICompress implementation
public long Compress(string file, string zipFile)
{
long length = -1;
FileStream ifs = null;
FileStream ofs = null;
try
{
ifs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
ofs = File.Open(zipFile, FileMode.Create, FileAccess.Write, FileShare.None);
DeflateStream dos = new DeflateStream(ofs, CompressionMode.Compress, true);
byte[] buff = new byte[ifs.Length];
while(true)
{
int r = ifs.Read(buff, 0, buff.Length);
if(r <= 0) break;
dos.Write(buff, 0, r);
}
dos.Flush();
dos.Close();
length = ofs.Length;
}
finally
{
if(ifs != null) ifs.Close();
if(ofs != null) ofs.Close();
}
return length;
}
示例9: BaseStream1
public void BaseStream1()
{
var writeStream = new MemoryStream();
var zip = new DeflateStream(writeStream, CompressionMode.Compress);
Assert.Same(zip.BaseStream, writeStream);
writeStream.Dispose();
}
示例10: BaseStream2
public void BaseStream2()
{
var ms = new MemoryStream();
var zip = new DeflateStream(ms, CompressionMode.Decompress);
Assert.Same(zip.BaseStream, ms);
ms.Dispose();
}
示例11: CompressCanWrite
public void CompressCanWrite()
{
var ms = new MemoryStream();
var zip = new DeflateStream(ms, CompressionMode.Compress);
Assert.True(zip.CanWrite);
zip.Dispose();
Assert.False(zip.CanWrite);
}
示例12: Embed
private string Embed(string prefix, string fullPath, bool compress)
{
var resourceName = String.Format("{0}{1}", prefix, Path.GetFileName(fullPath).ToLowerInvariant());
if (ModuleDefinition.Resources.Any(x => x.Name == resourceName))
{
LogInfo(string.Format("\tSkipping '{0}' because it is already embedded", fullPath));
return resourceName;
}
if (compress)
{
resourceName = String.Format("{0}{1}.zip", prefix, Path.GetFileName(fullPath).ToLowerInvariant());
}
LogInfo(string.Format("\tEmbedding '{0}'", fullPath));
var checksum = CalculateChecksum(fullPath);
var cacheFile = Path.Combine(cachePath, String.Format("{0}.{1}", checksum, resourceName));
var memoryStream = new MemoryStream();
if (File.Exists(cacheFile))
{
using (var fileStream = File.Open(cacheFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
fileStream.CopyTo(memoryStream);
}
}
else
{
using (var cacheFileStream = File.Open(cacheFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
{
using (var fileStream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
if (compress)
{
using (var compressedStream = new DeflateStream(memoryStream, CompressionMode.Compress, true))
{
fileStream.CopyTo(compressedStream);
}
}
else
{
fileStream.CopyTo(memoryStream);
}
}
memoryStream.Position = 0;
memoryStream.CopyTo(cacheFileStream);
}
}
memoryStream.Position = 0;
streams.Add(memoryStream);
var resource = new EmbeddedResource(resourceName, ManifestResourceAttributes.Private, memoryStream);
ModuleDefinition.Resources.Add(resource);
return resourceName;
}
示例13: DecompressCanRead
public void DecompressCanRead()
{
var ms = new MemoryStream();
var zip = new DeflateStream(ms, CompressionMode.Decompress);
Assert.True(zip.CanRead);
zip.Dispose();
Assert.False(zip.CanRead);
}
示例14: CanDisposeDeflateStream
public void CanDisposeDeflateStream()
{
var ms = new MemoryStream();
var zip = new DeflateStream(ms, CompressionMode.Compress);
zip.Dispose();
// Base Stream should be null after dispose
Assert.Null(zip.BaseStream);
zip.Dispose(); // Should be a no-op
}
示例15: Deflate
public static byte[] Deflate(this byte[] data)
{
using (MemoryStream output = new MemoryStream())
{
using (DeflateStream gzip = new DeflateStream(output, CompressionMode.Compress))
{
gzip.Write(data, 0, data.Length);
}
return output.ToArray();
}
}