Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a base class for a component that needs to handle data that is either uncompressed, PKZIP compressed (sharpziplib) or GZip compressed (built in .net). Perhaps a bit more than you need but should get you going. This is an example of using @PhonicUK's suggestion to parse the header of the data stream. The derived classes you see in the little factory mathod handled the specifics of PKZip and GZip decompression.</p> <pre><code>abstract class Expander { private const int ZIP_LEAD_BYTES = 0x04034b50; private const ushort GZIP_LEAD_BYTES = 0x8b1f; public abstract MemoryStream Expand(Stream stream); internal static bool IsPkZipCompressedData(byte[] data) { Debug.Assert(data != null &amp;&amp; data.Length &gt;= 4); // if the first 4 bytes of the array are the ZIP signature then it is compressed data return (BitConverter.ToInt32(data, 0) == ZIP_LEAD_BYTES); } internal static bool IsGZipCompressedData(byte[] data) { Debug.Assert(data != null &amp;&amp; data.Length &gt;= 2); // if the first 2 bytes of the array are theG ZIP signature then it is compressed data; return (BitConverter.ToUInt16(data, 0) == GZIP_LEAD_BYTES); } public static bool IsCompressedData(byte[] data) { return IsPkZipCompressedData(data) || IsGZipCompressedData(data); } public static Expander GetExpander(Stream stream) { Debug.Assert(stream != null); Debug.Assert(stream.CanSeek); stream.Seek(0, 0); try { byte[] bytes = new byte[4]; stream.Read(bytes, 0, 4); if (IsGZipCompressedData(bytes)) return new GZipExpander(); if (IsPkZipCompressedData(bytes)) return new ZipExpander(); return new NullExpander(); } finally { stream.Seek(0, 0); // set the stream back to the begining } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload