Exemple #1
0
        public override Stream GetResponseStream()
        {
            Stream compressed = null;

            var responseStream = _response.GetResponseStream();

            //#if !WindowsPhone
            if ((_response.Headers["Accept-Encoding"] != null && _response.Headers["Accept-Encoding"].Contains("gzip")) // Accept-Encoding? In responses?
                || _response.Headers["Content-Encoding"] != null && _response.Headers["Content-Encoding"].Contains("gzip"))
            {
                compressed = new GZipInputStream(responseStream);
            }
            else if ((_response.Headers["Accept-Encoding"] != null && _response.Headers["Accept-Encoding"].Contains("deflate"))
                    || (_response.Headers["Content-Encoding"] != null && _response.Headers["Content-Encoding"].Contains("deflate")))
            {
                compressed = new ZipInputStream(responseStream);
            }
            //#else
            if (compressed == null)
            {
                byte[] marker;
                responseStream = ReadIntoMemoryStream(responseStream, out marker);
                if (marker.Length > 2 && (marker[0] == 31 && marker[1] == 139))
                {
                    compressed = new GZipInputStream(responseStream);
                }
            }
            //#endif
            if (compressed != null)
            {
                var decompressed = new MemoryStream();
                var size = ChunkSize;
                var buffer = new byte[ChunkSize];
                while (true)
                {
                    size = compressed.Read(buffer, 0, size);
                    if (size > 0)
                    {
                        decompressed.Write(buffer, 0, size);
                    }
                    else
                    {
                        break;
                    }
                }
                decompressed.Seek(0, SeekOrigin.Begin);
                return decompressed;
            }

            return responseStream;
        }
Exemple #2
0
        public static async Task UnzipFromStream(Stream pZipStream,StorageFolder pDestinationFolder, List<string> pIgnore )
        {

            var zipInputStream = new ZipInputStream(pZipStream);
            var zipEntry = zipInputStream.GetNextEntry();
            while (zipEntry != null)
            {
               var entryFileName = (string)zipEntry.Name;
               
                byte[] buffer = new byte[4096]; 

                // Manipulate the output filename here as desired.

                var directoryName = Path.GetDirectoryName(entryFileName);
                var fileName = Path.GetFileName(entryFileName);
                if (!pIgnore.Contains(fileName.ToLower()))
                {

                    var outFolder = directoryName == null || directoryName.Length == 0
                        ? pDestinationFolder
                        : await GetOrCreateSubFolder(pDestinationFolder, directoryName);


                    StorageFile outFile = null;
                    try
                    {
                        outFile = await outFolder.GetFileAsync(fileName);
                    }
                    catch (Exception)
                    {

                    }
                    if (outFile == null)
                        outFile = await outFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                    using (var outStream = await outFile.OpenStreamForWriteAsync())
                    {
                        StreamUtils.Copy(zipInputStream, outStream, buffer);
                    }
                }
                zipEntry = zipInputStream.GetNextEntry();
            }
        }