Example #1
0
 public Setup(Uri uri, Repository repo)
 {
     using (var rs = _wc.OpenRead(new Uri(uri, string.Format("setup.dat?rnd={0}", new Random().Next())))) // remote access to compressed data
     using (var ds = new DeflateStream(rs, SharpCompress.Compressor.CompressionMode.Decompress)) // decompress (deflate)
         _doc.Load(ds); // load xml
     Repository = repo;
 }
Example #2
0
        static void Main(string[] args)
        {
            XmlDocument singleXml = new XmlDocument();
            singleXml.Load(args.Any() ? args.First() : "data.xml");

            var installationNode = singleXml.SelectSingleNode("//setup");
            using (var s = File.Create("setup.dat"))
            using (var ds = new DeflateStream(s, CompressionMode.Compress, CompressionLevel.BestCompression, false))
            using (var sw = new StreamWriter(ds))
            {
                sw.Write(installationNode.OuterXml);
                sw.Flush();
            }

            Directory.CreateDirectory("packages");

            foreach (
                XmlNode node
                in singleXml.SelectNodes("//repository/package").OfType<XmlNode>()
            )
            {
                string packageClass = node.Attributes["class"] == null ? null : node.Attributes["class"].Value;

                if (!string.IsNullOrEmpty(packageClass))
                {
                    var templateNode = singleXml.SelectSingleNode("//templates/package[@class='" + packageClass + "']");
                    foreach (var childNode in templateNode.ChildNodes.OfType<XmlNode>())
                        node.AppendChild(childNode.CloneNode(true));
                }

                using (var s = File.Create(Path.Combine("packages", string.Format("{0}.dat", node.Attributes["id"].Value))))
                using (var ds = new DeflateStream(s, CompressionMode.Compress, CompressionLevel.BestCompression, false))
                using (var sw = new StreamWriter(ds))
                {
                    sw.Write(node.OuterXml);
                    sw.Flush();
                }
            }
        }
Example #3
0
 private void initStream()
 {
     if ( deflateStream != null ) return;
     deflateStream = new DeflateStream ( rawStream, CompressionMode.Decompress, CompressionLevel.Default, true );
 }
Example #4
0
        private bool _ExtractTo( int index, Stream output, int outBufRemain )
        {
            XUnzipFileInfo fileInfo = _GetFileInfo ( index );

            BinaryReader br = new BinaryReader ( inputStream );

            if ( fileInfo.offsetData == -1 )
            {
                if ( inputStream.Seek ( fileInfo.offsetLocalHeader, SeekOrigin.Begin ) != fileInfo.offsetLocalHeader )
                    throw new Exception ( XUNZIP_ERR.CANT_READ_FILE.ToString () );
                if ( !ReadLocalHeader ( ref fileInfo, br ) )
                    return false;
            }

            inputStream.Seek ( fileInfo.offsetData, SeekOrigin.Begin );

            byte [] bufIn;
            int inputRemain;
            int outputRemain;
            int toRead;
            uint crc32 = 0;

            bufIn = new byte [ 32768 ];

            inputRemain = fileInfo.compressedSize;
            outputRemain = fileInfo.uncompressedSize;

            if ( fileInfo.method == XUNZIP_COMPRESSION_METHOD.METHOD_STORE )
            {
                CRC32 crcGen = new CRC32 ();
                while ( inputRemain > 0 )
                {
                    toRead = System.Math.Min ( System.Math.Min ( 32768, inputRemain ), outputRemain );

                    int temp = br.Read ( bufIn, 0, toRead );
                    output.Write ( bufIn, 0, toRead );
                    crcGen.Update ( bufIn, 0, toRead );
                    crc32 = crcGen.Result;

                    inputRemain -= toRead;
                    outputRemain -= temp;
                }
            }
            else
            {
                DeflateStream stream = new DeflateStream ( inputStream, CompressionMode.Decompress );

                CRC32 crcGen = new CRC32 ();
                while ( inputRemain > 0 )
                {
                    toRead = System.Math.Min ( System.Math.Min ( 32768, inputRemain ), outputRemain );

                    int temp = stream.Read ( bufIn, 0, outputRemain );
                    output.Write ( bufIn, 0, temp );
                    crcGen.Update ( bufIn, 0, toRead );
                    crc32 = crcGen.Result;

                    inputRemain -= toRead;
                    outputRemain -= temp;
                }
            }

            //if ( crc32 != fileInfo.crc32 )
            //return false;

            if ( outputRemain > 0 )
                return false;

            output.Position = 0;

            return true;
        }
        private XmlNode GetRemotePackageXmlNode(string package)
        {
            package = package.ToLower();

            XmlDocument doc = new XmlDocument();
            var uri = new Uri(RepositoryBase, string.Format("packages/{0}.dat?rnd={1}", package, new Random().Next()));
            Log.DebugFormat("Fetching remote package xml for {0} from {1}", package, uri);

            using (var rs = _wc.OpenRead(uri)) // remote access to compressed data
                using (var ds = new DeflateStream(rs, SharpCompress.Compressor.CompressionMode.Decompress)) // decompress (deflate)
                    doc.Load(ds); // load xml
            return doc.FirstChild; // <package...>...</package>
        }
        /// <summary>
        ///   Uncompress a DEFLATE'd byte array into a single string.
        /// </summary>
        ///
        /// <seealso cref="DeflateStream.CompressString(String)">DeflateStream.CompressString(String)</seealso>
        /// <seealso cref="DeflateStream.UncompressBuffer(byte[])">DeflateStream.UncompressBuffer(byte[])</seealso>
        /// <seealso cref="GZipStream.UncompressString(byte[])">GZipStream.UncompressString(byte[])</seealso>
        /// <seealso cref="ZlibStream.UncompressString(byte[])">ZlibStream.UncompressString(byte[])</seealso>
        ///
        /// <param name="compressed">
        ///   A buffer containing DEFLATE-compressed data.
        /// </param>
        ///
        /// <returns>The uncompressed string</returns>
        public static String UncompressString(byte[] compressed)
        {
            using (var input = new MemoryStream(compressed))
            {
                Stream decompressor =
                    new DeflateStream(input, CompressionMode.Decompress);

                return ZlibBaseStream.UncompressString(compressed, decompressor);
            }
        }
 /// <summary>
 ///   Compress a string into a byte array using DEFLATE (RFC 1951).
 /// </summary>
 ///
 /// <remarks>
 ///   Uncompress it with <see cref="DeflateStream.UncompressString(byte[])"/>.
 /// </remarks>
 ///
 /// <seealso cref="DeflateStream.UncompressString(byte[])">DeflateStream.UncompressString(byte[])</seealso>
 /// <seealso cref="DeflateStream.CompressBuffer(byte[])">DeflateStream.CompressBuffer(byte[])</seealso>
 /// <seealso cref="GZipStream.CompressString(string)">GZipStream.CompressString(string)</seealso>
 /// <seealso cref="ZlibStream.CompressString(string)">ZlibStream.CompressString(string)</seealso>
 ///
 /// <param name="s">
 ///   A string to compress. The string will first be encoded
 ///   using UTF8, then compressed.
 /// </param>
 ///
 /// <returns>The string in compressed form</returns>
 public static byte[] CompressString(String s)
 {
     using (var ms = new MemoryStream())
     {
         Stream compressor =
             new DeflateStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
         ZlibBaseStream.CompressString(s, compressor);
         return ms.ToArray();
     }
 }
Example #8
0
        private void initStream()
        {
            if ( deflateStream != null ) return;
            CompressionLevel clevel = CompressionLevel.BestCompression;

            if ( compressLevel >= 1 && compressLevel <= 5 ) clevel = CompressionLevel.BestSpeed;
            else if ( compressLevel == 0 ) clevel = CompressionLevel.None;
            deflateStream = new DeflateStream ( rawStream, CompressionMode.Compress, clevel, true );
        }