本文整理汇总了C#中Ionic.Zlib.ZlibStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# ZlibStream.Read方法的具体用法?C# ZlibStream.Read怎么用?C# ZlibStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ionic.Zlib.ZlibStream
的用法示例。
在下文中一共展示了ZlibStream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetStream
public Stream GetStream()
{
byte[] data = new byte[Data.Size];
long offset = Packfile.DataOffset + Data.Start;
Packfile.DataStream.Seek(offset, SeekOrigin.Begin);
if (Data.Flags.HasFlag(PackfileEntryFlags.Compressed))
{
byte[] compressedData = new byte[Data.CompressedSize];
Packfile.DataStream.Read(compressedData, 0, (int)Data.CompressedSize);
using (MemoryStream tempStream = new MemoryStream(compressedData))
{
using (Stream s = new ZlibStream(tempStream, CompressionMode.Decompress, true))
{
s.Read(data, 0, (int)Data.Size);
}
}
}
else
{
Packfile.DataStream.Read(data, 0, (int)Data.Size);
}
MemoryStream ms = new MemoryStream(data);
return ms;
}
示例2: Packfile
public Packfile(Stream stream, bool isStr2)
{
IsStr2 = isStr2;
stream.Seek(0, SeekOrigin.Begin);
FileData = stream.ReadStruct<PackfileFileData>();
m_Files = new List<IPackfileEntry>();
uint runningPosition = 0;
List<PackfileEntryFileData> entryFileData = new List<PackfileEntryFileData>();
for (int i = 0; i < FileData.NumFiles; i++)
{
PackfileEntryFileData data = stream.ReadStruct<PackfileEntryFileData>();
if (IsCondensed && IsCompressed)
{
data.Flags = 0;
data.Start = runningPosition;
runningPosition += data.Size;
}
else if (IsCondensed)
{
data.Start = runningPosition;
runningPosition += data.Size.Align(16);
}
entryFileData.Add(data);
}
for (int i = 0; i < FileData.NumFiles; i++)
{
stream.Align(2);
string filename = stream.ReadAsciiNullTerminatedString();
stream.Seek(1, SeekOrigin.Current);
m_Files.Add(new PackfileEntry(this, entryFileData[i], filename));
stream.Align(2);
}
if (IsCondensed && IsCompressed)
{
DataOffset = 0;
byte[] compressedData = new byte[FileData.CompressedDataSize];
stream.Read(compressedData, 0, (int)FileData.CompressedDataSize);
using (MemoryStream tempStream = new MemoryStream(compressedData))
{
using (Stream s = new ZlibStream(tempStream, CompressionMode.Decompress, true))
{
byte[] uncompressedData = new byte[FileData.DataSize];
s.Read(uncompressedData, 0, (int)FileData.DataSize);
DataStream = new MemoryStream(uncompressedData);
}
}
}
else
{
DataStream = stream;
DataOffset = stream.Position;
}
}
示例3: Decompress
/// <summary>
/// Decompress a byte array into another byte array of the specified size
/// </summary>
/// <param name="to_decompress">Data to decompress</param>
/// <param name="size_uncompressed">Size of the data once decompressed</param>
/// <returns>Decompressed data as a byte array</returns>
public static byte[] Decompress(byte[] to_decompress, int size_uncompressed)
{
ZlibStream stream = new ZlibStream(new System.IO.MemoryStream(to_decompress, false), CompressionMode.Decompress);
byte[] packetData_decompressed = new byte[size_uncompressed];
stream.Read(packetData_decompressed, 0, size_uncompressed);
stream.Close();
return packetData_decompressed;
}
示例4: DecompressResponse
public void DecompressResponse()
{
if (string.IsNullOrEmpty(this.ZNO))
{
return;
}
MemoryStream compressMetadataCollection = null;
MemoryStream decompressedMetadataCollection = null;
ZlibStream decompressStream = null;
try
{
byte[] compressedMetadataCollectionBytes = Convert.FromBase64String(this.ZNO);
compressMetadataCollection = new MemoryStream(compressedMetadataCollectionBytes);
byte[] buffer = new byte[1024];
int numBytesRead = 0;
bool start = true;
decompressedMetadataCollection = new MemoryStream();
using (decompressStream = new ZlibStream(compressMetadataCollection, CompressionMode.Decompress))
{
while (start || numBytesRead > 0)
{
numBytesRead = decompressStream.Read(buffer, 0, buffer.Length);
if (numBytesRead > 0)
{
decompressedMetadataCollection.Write(buffer, 0, numBytesRead);
}
start = false;
}
}
decompressedMetadataCollection.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(typeof(CompressedResponseTuple));
CompressedResponseTuple responseTuple = deserializer.ReadObject(decompressedMetadataCollection) as CompressedResponseTuple;
Nodes = responseTuple.Nodes;
Relationships = responseTuple.Relationships;
}
finally
{
if (decompressedMetadataCollection != null)
{
decompressedMetadataCollection.Dispose();
decompressedMetadataCollection = null;
}
}
}
示例5: Compress
/// <summary>
/// Compress a byte array into another bytes array using Zlib compression
/// </summary>
/// <param name="to_compress">Data to compress</param>
/// <returns>Compressed data as a byte array</returns>
public static byte[] Compress(byte[] to_compress)
{
ZlibStream stream = new ZlibStream(new System.IO.MemoryStream(to_compress, false), CompressionMode.Compress);
List<byte> temp_compression_list = new List<byte>();
byte[] b = new byte[1];
while (true)
{
int read = stream.Read(b, 0, 1);
if (read > 0) { temp_compression_list.Add(b[0]); }
else break;
}
stream.Close();
return temp_compression_list.ToArray();
}
示例6: Gunz2Packet
public UInt16 pktID2; //packet id again
#endregion Fields
#region Constructors
public Gunz2Packet(byte[] buf, byte[] _cryptKey)
{
flagsraw = BitConverter.ToUInt32(buf, 0);
flags = new Gunz2Flags();
flags.keepalive = (byte)((flagsraw >> 0) & 1) == 1;
flags.isping = (byte)((flagsraw >> 1) & 1) == 1; //used for keepalive. Normal is set to false for keepalive packets.
flags.unkFlag3 = (byte)((flagsraw >> 2) & 1) == 1;
flags.encrypted = (byte)((flagsraw >> 3) & 1) == 1;
flags.compressed = (byte)((flagsraw >> 4) & 1) == 1;
flags.size = (uint)(0x7FFFFF & (flagsraw >> 5));
// how to check if it it's encrypted.
if (flags.encrypted)
{
Decrypt(buf, 12, (uint)buf.Length - 12, _cryptKey);
}
if (flags.compressed && flags.encrypted && flags.size < 65535)
{
uint fullSize = BitConverter.ToUInt32(buf, 12);
byte[] decompressedBuffer = new byte[fullSize - 12];
byte[] cpyArray = new byte[flags.size - 16];
Buffer.BlockCopy(buf, 16, cpyArray, 0, buf.Length - 16);
//Console.WriteLine("This packet is compressed. Length: {0}", fullSize - 12);
using (ZlibStream zlib = new ZlibStream(new MemoryStream(cpyArray), CompressionMode.Decompress))
{
zlib.Read(decompressedBuffer, 0, (int)fullSize - 12);
}
Array.Resize(ref buf, (int)fullSize);
Buffer.BlockCopy(decompressedBuffer, 0, buf, 12, (int)fullSize - 12);
}
data = buf;
if (!flags.isping)
{
pktCounter = BitConverter.ToUInt32(data, 0);
pktID = BitConverter.ToUInt16(data, 8);
checksum = BitConverter.ToUInt16(data, 10);
datalen = BitConverter.ToUInt32(data, 12);
pktID2 = BitConverter.ToUInt16(data, 16);
}
else
{
//pingpong
}
}
示例7: Decompress
/// <summary>
/// Decompresses a byte array using the specified compression type.
/// </summary>
/// <param name="bytes">The byte array to be decompressed.</param>
/// <param name="type">Type of compression to use.</param>
/// <returns>Decompressed byte array.</returns>
public static byte[] Decompress(this byte[] bytes, CompressionType type)
{
int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
using (MemoryStream memory2 = new MemoryStream(bytes))
switch (type)
{
case CompressionType.Zlib:
using (ZlibStream stream = new ZlibStream(memory2, CompressionMode.Decompress))
{
int count = 0;
while ((count = stream.Read(buffer, 0, size)) > 0)
memory.Write(buffer, 0, count);
}
break;
case CompressionType.GZip:
using (GZipStream stream = new GZipStream(memory2, CompressionMode.Decompress))
{
int count = 0;
while ((count = stream.Read(buffer, 0, size)) > 0)
memory.Write(buffer, 0, count);
}
break;
default:
throw new ArgumentException("Unknown compression type.");
}
return memory.ToArray();
}
}
示例8: ProcessFirmware
public static Firmware ProcessFirmware(string path)
{
// JavaScriptSerializer serializer = new JavaScriptSerializer();
Console.WriteLine("Read File " + path);
// read the file
StreamReader f = new StreamReader(File.OpenRead(path));
//fw = serializer.Deserialize<Firmware>(f.ReadToEnd());
fw = JSON.Instance.ToObject<Firmware>(f.ReadToEnd());
f.Close();
byte[] data = Convert.FromBase64String(fw.image);
MemoryStream imagems = new MemoryStream(data, true);
ZlibStream decompressionStream = new ZlibStream(imagems, CompressionMode.Decompress);
int size = fw.image_size + (fw.image_size % 4);
fw.imagebyte = new byte[size];
for (int a = 0; a < fw.imagebyte.Length; a++)
{
fw.imagebyte[a] = 0xff;
}
try
{
decompressionStream.Read(fw.imagebyte, 0, fw.image_size);
}
catch { Console.WriteLine("Possible bad file - usualy safe to ignore"); }
Console.WriteLine("image_size {0} size {1}",fw.image_size,size);
BinaryWriter sw = new BinaryWriter(File.Open("px4fw.bin", FileMode.Create));
foreach (byte by in fw.imagebyte)
{
sw.Write(by);
// Console.Write("{0:x2}", by);
}
sw.Close();
// pad image to 4-byte length
//while ((fw.imagebyte.Length % 4) != 0) {
//fw.imagebyte. += b'\x00'
return fw;
}
示例9: run
public void run()
{
try
{
for (;;)
{
if (!this.client.connectionAlive)
{
this.client.forceDisconnect(direction, "Connection no longer alive");
return;
}
if (this.client.kickTargetTimestamp != 0)
{
if (this.client.kickTargetTimestamp < Utils.getTimestamp())
{
this.client.closeConnection();
return;
}
continue;
}
#region Process Packet
//Packet ID and Vaildity Check.
uint temp = this.incoming.ReadVarUInt32();
if (temp < 1 || temp > 48)
{
this.client.forceDisconnect(direction, "Sent invalid packet ID [" + temp + "].");
return;
}
Packet packetID = (Packet)temp;
//Packet Size and Compression Check.
bool compressed = false;
int packetSize = this.incoming.ReadVarInt32();
if (packetSize < 0)
{
packetSize = -packetSize;
compressed = true;
}
//Create buffer for forwarding
byte[] dataBuffer = this.incoming.ReadFully(packetSize);
//Do decompression
MemoryStream ms = new MemoryStream();
if (compressed)
{
ZlibStream compressedStream = new ZlibStream(new MemoryStream(dataBuffer), CompressionMode.Decompress);
byte[] buffer = new byte[32768];
for (;;)
{
int read = compressedStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
break;
ms.Write(buffer, 0, read);
}
ms.Seek(0, SeekOrigin.Begin);
}
else
{
ms = new MemoryStream(dataBuffer);
}
//Create packet parser
BinaryReader packetData = new BinaryReader(ms);
#endregion
//Return data for packet processor
object returnData = true;
if (packetID != Packet.Heartbeat && packetID != Packet.UniverseTimeUpdate)
{
if (direction == Direction.Client)
#region Handle Client Packets
{
#region Protocol State Security
ClientState curState = this.client.state;
if (curState != ClientState.Connected)
{
if (curState == ClientState.PendingConnect && packetID != Packet.ClientConnect)
{
this.client.rejectPreConnected("Violated PendingConnect protocol state with " + packetID);
return;
}
else if (curState == ClientState.PendingAuthentication && packetID != Packet.HandshakeResponse)
{
this.client.rejectPreConnected("Violated PendingAuthentication protocol state with " + packetID);
return;
}
else if (curState == ClientState.PendingConnectResponse)
{
int startTime = Utils.getTimestamp();
while (true)
{
if (this.client.state == ClientState.Connected) break;
if (Utils.getTimestamp() > startTime + StarryboundServer.config.connectTimeout)
{
this.client.rejectPreConnected("Connection Failed: Server did not respond in time.");
return;
//.........这里部分代码省略.........
示例10: FillImageContent
private void FillImageContent()
{
byte[] rawContent = GetRawContent();
// HACK: Detect compressed images. In reality there should be some way to determine
// this from the first 32 bytes, but I can't see any similarity between all the
// samples I have obtained, nor any similarity in the data block contents.
if (MatchSignature(rawContent, COMPRESSED1, 32) || MatchSignature(rawContent, COMPRESSED2, 32))
{
try
{
ZlibStream gzip = new ZlibStream(new MemoryStream(rawContent, 33, rawContent.Length - 33), CompressionMode.Decompress);
MemoryStream out1 = new MemoryStream();
byte[] buf = new byte[4096];
int readBytes;
while ((readBytes = gzip.Read(buf,0,4096)) > 0)
{
out1.Write(buf, 0, readBytes);
}
content = out1.ToArray();
}
catch (IOException)
{
// Problems Reading from the actual MemoryStream should never happen
// so this will only ever be a ZipException.
//log.log(POILogger.INFO, "Possibly corrupt compression or non-compressed data", e);
}
} else {
// Raw data is not compressed.
content = rawContent;
}
}
示例11: readFromStreamOld
private void readFromStreamOld(Stream s)
{
using (var tr = new StreamReader(s, System.Text.Encoding.ASCII))
{
// Defaults
bool deflate = true;
bool base64 = true;
_readOnly = false;
int contLength = -1;
// These are used to control big/little endian reading mode.
int lb = 0, hb = 1;
string line;
while ((line = tr.ReadLine()) != null)
{
line = line.Trim();
if (line.Length == 0)
break;
var parts = line.Split(headerSeps, 2, StringSplitOptions.None);
switch (parts[0].ToLower())
{
case "access":
if (parts.Length > 1)
{
switch (parts[1].ToLower())
{
case "read-only":
_readOnly = true;
break;
case "read-write":
_readOnly = false;
break;
}
}
break;
case "byte-order":
if (parts.Length > 1)
{
switch (parts[1].ToLower())
{
case "little-endian":
lb = 0;
hb = 1;
break;
case "big-endian":
lb = 1;
hb = 0;
break;
}
}
break;
case "compression":
if (parts.Length > 1)
{
switch (parts[1].ToLower())
{
case "none":
deflate = false;
break;
case "zlib":
deflate = true;
break;
}
}
break;
case "encoding":
if (parts.Length > 1)
{
switch (parts[1].ToLower())
{
case "none":
base64 = false;
break;
case "base64":
base64 = true;
break;
}
}
break;
case "content-length":
if (parts.Length > 1)
{
if (!int.TryParse(parts[1], out contLength))
contLength = -1;
}
break;
}
}
//.........这里部分代码省略.........
示例12: CreateObject
public static IObject CreateObject(string path)
{
logger.Trace("creating new Object from path {0}", path);
string digestString = Path.GetFileName(Path.GetDirectoryName(path)) + Path.GetFileName(path);
logger.Trace("filename indicates digest of: {0}", digestString);
byte[] digest = Encoding.ASCII.GetBytes(digestString.ToCharArray());
byte[] buffer = new byte[MaxHeaderLength];
using (ZlibStream input = new ZlibStream(File.Open(path, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))
{
logger.Trace("reading {0} bytes", MaxHeaderLength);
input.Read(buffer, 0, MaxHeaderLength);
}
string[] header = null;
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] == 0)
{
header = new String(Encoding.ASCII.GetChars(buffer, 0, i)).Split(' ');
break;
}
}
logger.Trace("got header: {0} {1}", header[0], header[1]);
logger.Debug("got object of type {0}", header[0]);
// TODO use header to create appropriate object
Object result = null;
switch (header[0])
{
case "blob":
var blob = new Blob();
blob.m_Type = ObjectType.Blob;
result = blob;
break;
case "tree":
var tree = new Tree();
tree.m_Type = ObjectType.Tree;
result = tree;
break;
case "commit":
var commit = new Commit();
commit.m_Type = ObjectType.Commit;
result = commit;
break;
case "tag":
break;
default:
break;
}
result.m_Path = path;
result.m_Digest = digest;
result.m_Size = Convert.ToUInt64(header[1]);
result.m_HeaderLen = header[0].Length + header[1].Length + 2; // + 2 to account for the ' ' and '\0'.
return result;
}
示例13: Deflate
public void Deflate(string outputPath)
{
using (ZlibStream input = new ZlibStream(File.Open(m_Path, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))
{
int maxBlockSize;
logger.Trace("Opening registry key to find MaxBlockSize");
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Bit Thicket\\Git\\Streams");
try
{
maxBlockSize = Convert.ToInt32(key.GetValue("MaxBlockSize"));
}
catch (Exception e)
{
logger.LogException(LogLevel.Info, "MaxBlockSize not specified. Using default of 4k", e);
maxBlockSize = 4096;
}
using (var t0 = new TransferStream(File.Open(outputPath, FileMode.Create, FileAccess.Write)))
{
byte[] buffer = new byte[maxBlockSize];
int readCount = 0;
input.Read(buffer, 0, (int)m_HeaderLen);
while (maxBlockSize == (readCount = input.Read(buffer, 0, maxBlockSize)))
{
logger.Trace("read {0} bytes", readCount);
t0.Write(buffer, 0, readCount);
}
logger.Trace("writing final block of {0} bytes", readCount);
t0.Write(buffer, 0, readCount);
}
}
}
示例14: DecompressZlib
public static byte[] DecompressZlib(Stream input)
{
using (var decompressor = new ZlibStream(input, CompressionMode.Decompress))
{
var buffer = new byte[BufferSize];
using (var output = new MemoryStream())
{
int read;
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
return output.ToArray();
}
}
}
示例15: DecompressBulkOperations
private BulkOperations DecompressBulkOperations(string compressedOperations)
{
MemoryStream compressedOperationsStream = null;
MemoryStream decompressedOperationsStream = null;
ZlibStream decompressionStream = null;
try
{
byte[] compressedOperationsBytes = Convert.FromBase64String(compressedOperations);
compressedOperationsStream = new MemoryStream(compressedOperationsBytes);
byte[] buffer = new byte[1024];
int numBytesRead = 0;
bool start = true;
decompressedOperationsStream = new MemoryStream();
using (decompressionStream = new ZlibStream(compressedOperationsStream, CompressionMode.Decompress))
{
while (start || numBytesRead > 0)
{
numBytesRead = decompressionStream.Read(buffer, 0, buffer.Length);
if (numBytesRead > 0)
{
decompressedOperationsStream.Write(buffer, 0, numBytesRead);
}
start = false;
}
}
decompressedOperationsStream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(typeof(BulkOperations));
BulkOperations operations = deserializer.ReadObject(decompressedOperationsStream) as BulkOperations;
return operations;
}
finally
{
if (decompressedOperationsStream != null)
{
decompressedOperationsStream.Dispose();
decompressedOperationsStream = null;
}
}
}