Пример #1
0
        private async Task DownloadArtifactToArchiveAsync(DumpArtifact dumpArtifact, ZipArchive archive, SemaphoreSlim archiveLock, CancellationToken cancelToken)
        {
            if (!cancelToken.IsCancellationRequested)
            {
                var blob = DumplingStorageClient.BlobClient.GetBlobReferenceFromServer(new Uri(dumpArtifact.Artifact.Url));

                //download the compressed dump artifact to a temp file
                using (var tempStream = CreateTempFile())
                {
                    using (var compStream = CreateTempFile())
                    {
                        await blob.DownloadToStreamAsync(compStream, cancelToken);

                        using (var gunzipStream = new GZipStream(compStream, CompressionMode.Decompress, false))
                        {
                            await gunzipStream.CopyToAsync(tempStream);
                        }

                        await tempStream.FlushAsync();
                    }

                    tempStream.Position = 0;

                    await archiveLock.WaitAsync(cancelToken);

                    if (!cancelToken.IsCancellationRequested)
                    {
                        try
                        {
                            var entry = archive.CreateEntry(FixupLocalPath(dumpArtifact.LocalPath));

                            using (var entryStream = entry.Open())
                            {
                                await tempStream.CopyToAsync(entryStream);

                                await entryStream.FlushAsync();
                            }
                        }
                        finally
                        {
                            archiveLock.Release();
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 对流进行解压
        /// </summary>
        /// <param name="response"></param>
        private static void UnGZip(HttpResponseMessage response)
        {
            bool isGzip = response.Content.Headers.ContentEncoding.Contains("gzip");

            if (isGzip)
            {
                Stream decompressedStream = new MemoryStream();
                using (var gzipStream = new GZipStream(response.Content.ReadAsStreamAsync().Result, CompressionMode.Decompress))
                {
                    gzipStream.CopyToAsync(decompressedStream);
                }
                decompressedStream.Seek(0, SeekOrigin.Begin);

                var originContent = response.Content;
                response.Content = new StreamContent(decompressedStream);
            }
        }
Пример #3
0
        /// <summary>
        /// static|Downloads an xml file from AniDB which contains all of the titles for every anime, and their IDs,
        /// and saves it to disk.
        /// </summary>
        /// <param name="titlesFile"></param>
        /// <returns></returns>
        private static async Task DownloadTitles_static(string titlesFile)
        {
            var client = new WebClient();

            client.Headers.Add("User-Agent", Constants.UserAgent);

            await AniDbSeriesProvider.RequestLimiter.Tick().ConfigureAwait(false);

            await Task.Delay(Plugin.Instance.Configuration.AniDbRateLimit).ConfigureAwait(false);

            using (var stream = await client.OpenReadTaskAsync(TitlesUrl))
                using (var unzipped = new GZipStream(stream, CompressionMode.Decompress))
                    using (var writer = File.Open(titlesFile, FileMode.Create, FileAccess.Write))
                    {
                        await unzipped.CopyToAsync(writer).ConfigureAwait(false);
                    }
        }
Пример #4
0
        /// <summary>
        /// Downloads an xml file from AniDB which contains all of the titles for every anime, and their IDs,
        /// and saves it to disk.
        /// </summary>
        /// <param name="titlesFile">The destination file name.</param>
        private async Task DownloadTitles(string titlesFile)
        {
            _logger.LogDebug("Downloading new AniDB titles file.");

            var client = new WebClient();

            await AniDbSeriesProvider.RequestLimiter.Tick().ConfigureAwait(false);

            await Task.Delay(Plugin.Instance.Configuration.AniDB_wait_time).ConfigureAwait(false);

            using (var stream = await client.OpenReadTaskAsync(TitlesUrl))
                using (var unzipped = new GZipStream(stream, CompressionMode.Decompress))
                    using (var writer = File.Open(titlesFile, FileMode.Create, FileAccess.Write))
                    {
                        await unzipped.CopyToAsync(writer).ConfigureAwait(false);
                    }
        }
Пример #5
0
    public async Task MutateIncoming(MutateIncomingTransportMessageContext context)
    {
        if (!context.Headers.ContainsKey("IWasCompressed"))
        {
            return;
        }
        var memoryStream = new MemoryStream(context.Body);

        using (var bigStream = new GZipStream(memoryStream, CompressionMode.Decompress))
        {
            var bigStreamOut = new MemoryStream();
            await bigStream.CopyToAsync(bigStreamOut)
            .ConfigureAwait(false);

            context.Body = bigStreamOut.ToArray();
        }
    }
Пример #6
0
        public async Task Accept(INodeVisitor visitor)
        {
            // Extract the file to a temporary file, and then parse that.
            using (var tempFile = new TempFile())
            {
                // Extract to the temporary file
                using (var tempStream = tempFile.OpenWrite())
                    using (var stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
                        using (var gzipFile = new GZipStream(stream, CompressionMode.Decompress, false))
                        {
                            await gzipFile.CopyToAsync(tempStream);
                        }

                // Process the extracted file
                await visitor.Visit(new FileNode(tempFile.File));
            }
        }
Пример #7
0
 public void Decompress(string compressedFile, string targetFile, ProgressBar bar)
 {
     bar.Value = 0;
     try
     {
         new Task(() =>
         {
             if (File.Exists(compressedFile))
             {
                 using (FileStream sourceStream = new FileStream(compressedFile, FileMode.OpenOrCreate))
                 {
                     using (FileStream targetStream = File.Create(targetFile))
                     {
                         using (GZipStream decompressionStream = new GZipStream(sourceStream, CompressionMode.Decompress))
                         {
                             new Task(() =>
                             {
                                 long compressed_length = new System.IO.FileInfo(compressedFile).Length;
                                 long target_length     = new System.IO.FileInfo(targetFile).Length;
                                 if ((target_length / 2) < compressed_length)
                                 {
                                     bar.Value += 100;
                                 }
                             }).Start();
                             decompressionStream.CopyToAsync(targetStream).Wait();
                             MessageBox.Show("File has been decompressed!");
                             bar.Value = 0;
                         }
                     }
                 }
             }
             else
             {
                 throw new Exception("Compressed file doesn't exist");
             }
         }).Start();
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
     }
 }
Пример #8
0
        private static async Task RunWithoutStreamingApiAsync(FileInfo file, int totalObjects, bool iswarmup = true)
        {
            long beforeMem = 0;
            long afterMem  = 0;

            if (iswarmup)
            {
                beforeMem = Helpers.GetMemoryPostGc() >> 10;
            }
            var sw           = Stopwatch.StartNew();
            var byteData     = File.ReadAllBytes(file.FullName);
            var unzippedData = new MemoryStream();

            using (var unzipper = new GZipStream(new MemoryStream(byteData), CompressionMode.Decompress, false))
            {
                await unzipper.CopyToAsync(unzippedData).ConfigureAwait(false);
            }

            var deserializedJson =
                JsonConvert.DeserializeObject <List <MyTestData> >(new UTF8Encoding().GetString(unzippedData.ToArray()));

            sw.Stop();
            if (iswarmup)
            {
                afterMem = Helpers.GetMemoryPostGc() >> 10;
            }
            Thread.MemoryBarrier();
            if (deserializedJson.Count != totalObjects)
            {
                throw new Exception($"Expected {totalObjects} objects. Got only {deserializedJson.Count}");
            }

            if (iswarmup)
            {
                await Console.Out.WriteLineAsync($"NonAPI Memory => Before: {beforeMem} KB, After: {afterMem} KB, " +
                                                 $"Diff: {afterMem - beforeMem} KB")
                .ConfigureAwait(false);
                await RunWithoutStreamingApiAsync(file, totalObjects, false).ConfigureAwait(false);
            }
            else
            {
                await Console.Out.WriteLineAsync($"NonAPI Total Time:{sw.ElapsedMilliseconds} ms")
                .ConfigureAwait(false);
            }
        }
        public async Task CompressionThresholdZeroGZipNoCompressionTest()
        {
            // Arrange
            using (
                var server =
                    new TestServer(
                        this.CreateBuilder(options =>
            {
                options.MinimumCompressionThreshold = 0;
                options.Compressors = new List <ICompressor> {
                    new GZipCompressor(CompressionLevel.NoCompression)
                };
            })))
            {
                // Act
                RequestBuilder request = server.CreateRequest("/");
                request.AddHeader(HeaderNames.AcceptEncoding, "gzip");

                HttpResponseMessage response = await request.SendAsync("PUT");

                // Assert
                response.EnsureSuccessStatusCode();

                Stream stream = await response.Content.ReadAsStreamAsync();

                string responseText;

                using (var decompression = new GZipStream(stream, CompressionMode.Decompress))
                {
                    using (var ms = new MemoryStream())
                    {
                        await decompression.CopyToAsync(ms);

                        responseText = Encoding.UTF8.GetString(ms.ToArray());
                    }
                }

                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "StatusCode != OK");
                Assert.AreEqual(Helpers.ResponseText, responseText, "Response Text not equal");
                Assert.AreEqual(147, response.Content.Headers.ContentLength, "Content-Length != 147");
                Assert.AreEqual(true, response.Content.Headers.ContentEncoding.Any(), "Content-Encoding == null");
                Assert.AreEqual("gzip", response.Content.Headers.ContentEncoding.ToString(), "Content-Encoding != gzip");
                Assert.AreEqual(true, response.Headers.Vary.Contains(HeaderNames.AcceptEncoding), "Vary != Accept-Encoding");
            }
        }
Пример #10
0
        public static async Task UnzipAsync <TDataSet>(this IModelLoader <TDataSet> modelLoader, string zipFile, string saveTo, bool showProgressInConsole = false)
            where TDataSet : IDataSet
        {
            if (!Path.IsPathRooted(saveTo))
            {
                saveTo = Path.Combine(AppContext.BaseDirectory, saveTo);
            }

            Directory.CreateDirectory(saveTo);

            if (!Path.IsPathRooted(zipFile))
            {
                zipFile = Path.Combine(AppContext.BaseDirectory, zipFile);
            }

            var destFileName = Path.GetFileNameWithoutExtension(zipFile);
            var destFilePath = Path.Combine(saveTo, destFileName);

            if (showProgressInConsole)
            {
                Console.WriteLine($"Unzippinng {Path.GetFileName(zipFile)}");
            }

            if (File.Exists(destFilePath))
            {
                if (showProgressInConsole)
                {
                    Console.WriteLine($"The file {destFileName} already exists");
                }
            }

            using (GZipStream unzipStream = new GZipStream(File.OpenRead(zipFile), CompressionMode.Decompress))
            {
                using (var destStream = File.Create(destFilePath))
                {
                    await unzipStream.CopyToAsync(destStream).ConfigureAwait(false);

                    await destStream.FlushAsync().ConfigureAwait(false);

                    destStream.Close();
                }

                unzipStream.Close();
            }
        }
Пример #11
0
 public static async Task WriteFlotAppAsync(Stream output, bool decompress = false)
 {
     if (!decompress)
     {
         using (var stream = Assembly.GetAssembly(typeof(FlotWebApp)).GetManifestResourceStream("Metrics.Visualization.index.full.html.gz"))
         {
             await stream.CopyToAsync(output).ConfigureAwait(false);
         }
     }
     else
     {
         using (var stream = Assembly.GetAssembly(typeof(FlotWebApp)).GetManifestResourceStream("Metrics.Visualization.index.full.html.gz"))
             using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
             {
                 await gzip.CopyToAsync(output).ConfigureAwait(false);
             }
     }
 }
Пример #12
0
        /// <summary>
        /// Gets the uncompressed string.
        /// </summary>
        /// <param name="input">The input byte array.</param>
        /// <returns>Returns the uncompressed string.</returns>
        private async Task <string> DecompressAsync(byte[] input)
        {
            if (input == null)
            {
                return(string.Empty);
            }

            using (MemoryStream mso = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(input))
                    using (GZipStream zs = new GZipStream(ms, CompressionMode.Decompress, true))
                    {
                        await zs.CopyToAsync(mso).ConfigureAwait(false);
                    }

                return(Encoding.UTF8.GetString(mso.ToArray()));
            }
        }
Пример #13
0
        /// <summary>
        /// Returns the decompressed version of provided data.
        /// </summary>
        /// <returns>The decompressed data stream.</returns>
        /// <exception cref="ArgumentNullException">The source or destination are null.</exception>
        public static async ValueTask <TStream> DecompressAsync <TStream>(Stream source, TStream destination)
            where TStream : Stream
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination is null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            Contract.EndContractBlock();

            using var gs = new GZipStream(source, CompressionMode.Decompress);
            await gs.CopyToAsync(destination).ConfigureAwait(false);

            return(destination);
        }
        public static async Task <byte[]> GzipDecompressAsync(this byte[] bytes)
        {
            byte[] decompressedBytes;

            using (var inputStream = new MemoryStream(bytes))
                using (var outputStream = new MemoryStream())
                {
                    //GZipStream must be disposed to ensure that all data is flushed!
                    using (var zipStream = new GZipStream(inputStream, CompressionMode.Decompress))
                    {
                        await zipStream.CopyToAsync(outputStream).ConfigureAwait(false);
                    }

                    decompressedBytes = outputStream.ToArray();
                }

            return(decompressedBytes);
        }
Пример #15
0
        private static async Task ReadContent(HttpWebResponse response, HttpRes res)
        {
            res.ResponseChartSet = response.CharacterSet;

            // 不能依赖content-length
            using (var mem = new MemoryStream())
            {
                using (var stream = response.GetResponseStream())
                {
                    if (response.ContentEncoding?.ToLowerInvariant() == "gzip")
                    {
                        using (var gZipStream = new GZipStream(stream, CompressionMode.Decompress))
                            await gZipStream.CopyToAsync(mem).DonotCapture();
                    }
                    else
                    {
                        await stream.CopyToAsync(mem).DonotCapture();
                    }
                }

                switch (res.Req.ResultType)
                {
                case HttpResultType.String:
                    var charset = res.Req.ResultCharSet.IsNullOrEmpty()
                            ? response.CharacterSet
                            : res.Req.ResultCharSet;

                    var contentEncoding = charset.IsNullOrEmpty()
                            ? Encoding.UTF8
                            : Encoding.GetEncoding(charset); // GetEncoding returns a cached instance with default settings.

                    mem.Seek(0, SeekOrigin.Begin);
                    using (var sr = new StreamReader(mem, contentEncoding))
                    {
                        res.ResponseString = sr.ReadToEnd();
                    }
                    break;

                case HttpResultType.Byte:
                    res.ResponseBytes = mem.ToArray();
                    break;
                }
            }
        }
Пример #16
0
        private async Task <BoolResult> StreamContentWithCompressionAsync(IAsyncStreamReader <CopyFileResponse> input, Stream output, CopyOptions?options, CancellationToken cancellationToken)
        {
            try
            {
                long chunks = 0L;
                long bytes  = 0L;

                using (var grpcStream = new BufferedReadStream(
                           async() =>
                {
                    if (await input.MoveNext(cancellationToken))
                    {
                        if (input.Current.Header is { } header)
                        {
                            var error = new ErrorResult(header.ErrorMessage ?? "Unknown error", header.Diagnostics);
                            error.ThrowIfFailure();
                        }

                        chunks++;
                        bytes += input.Current.Content.Length;

                        options?.UpdateTotalBytesCopied(bytes);

                        return(input.Current.Content);
                    }
                    else
                    {
                        return(null);
                    }
                }))
                {
                    using (Stream decompressedStream = new GZipStream(grpcStream, CompressionMode.Decompress, true))
                    {
                        await decompressedStream.CopyToAsync(output, _configuration.ClientBufferSizeBytes, cancellationToken);
                    }
                }

                return(BoolResult.Success);
            }
            catch (ResultPropagationException e)
            {
                return(new BoolResult(e.Result));
            }
        }
Пример #17
0
        //  load
        protected override async Task <StorageContent> OnLoad(Uri resourceUri, CancellationToken cancellationToken)
        {
            // the Azure SDK will treat a starting / as an absolute URL,
            // while we may be working in a subdirectory of a storage container
            // trim the starting slash to treat it as a relative path
            string name = GetName(resourceUri).TrimStart('/');

            CloudBlockBlob blob = _directory.GetBlockBlobReference(name);

            if (blob.Exists())
            {
                MemoryStream originalStream = new MemoryStream();
                await blob.DownloadToStreamAsync(originalStream, cancellationToken);

                originalStream.Position = 0;

                MemoryStream content;

                if (blob.Properties.ContentEncoding == "gzip")
                {
                    using (var uncompressedStream = new GZipStream(originalStream, CompressionMode.Decompress))
                    {
                        content = new MemoryStream();

                        await uncompressedStream.CopyToAsync(content);
                    }

                    content.Position = 0;
                }
                else
                {
                    content = originalStream;
                }

                return(new StreamStorageContent(content));
            }

            if (Verbose)
            {
                _logger.LogInformation("Can't load {BlobUri}. Blob doesn't exist", resourceUri);
            }

            return(null);
        }
Пример #18
0
        /// <summary>
        ///     提取网页Byte
        /// </summary>
        /// <returns></returns>
        private async Task <byte[]> GetByte(HttpItem HttpDataItem, HttpResponseMessage response)
        {
            byte[] ResponseByte = null;

            try
            {
                ResponseByte = await response.Content.ReadAsByteArrayAsync();
            }
            catch (WebException ex)
            {
                LogHelper.Log(LogDefine.LogError, ex, $"获取http返回流异常,{HttpDataItem.URL}");
                var exResponse = (HttpWebResponse)ex.Response;
                if (exResponse == null)
                {
                    return(null);
                }
                MemoryStream _stream = null;
                try
                {
                    using var stream = exResponse.GetResponseStream();
                    if (stream != null)
                    {
                        _stream = new MemoryStream();
                        if (exResponse.ContentEncoding.Equals("gzip", StringComparison.OrdinalIgnoreCase))
                        {
                            using var item = new GZipStream(stream, CompressionMode.Decompress);
                            await item.CopyToAsync(_stream, 10240);
                        }
                        else
                        {
                            await stream.CopyToAsync(_stream, 10240);
                        }

                        ResponseByte = _stream.ToArray();
                    }
                }
                catch
                {
                    _stream?.Dispose();
                }
            }

            return(ResponseByte);
        }
        public static async Task <string> UnpackByteToJsonString(byte[] buffer)
        {
            string jsonString;

            using (var compressedStream = new MemoryStream(buffer))
            {
                using (var zipStream = new GZipStream((Stream)compressedStream, CompressionMode.Decompress))
                {
                    using (var resultStream = new MemoryStream())
                    {
                        await zipStream.CopyToAsync((Stream)resultStream).ConfigureAwait(false);

                        jsonString = Encoding.UTF8.GetString(resultStream.ToArray());
                    }
                }
            }

            return(jsonString);
        }
Пример #20
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                      CancellationToken cancellationToken)
        {
            if (request.Method == HttpMethod.Post)
            {
                bool isGzip    = request.Content.Headers.ContentEncoding.Contains("gzip");
                bool isDeflate = !isGzip && request.Content.Headers.ContentEncoding.Contains("deflate");

                if (isGzip || isDeflate)
                {
                    Stream decompressedStream = new MemoryStream();

                    if (isGzip)
                    {
                        using (var gzipStream = new GZipStream(await request.Content.ReadAsStreamAsync(),
                                                               CompressionMode.Decompress))
                        {
                            await gzipStream.CopyToAsync(decompressedStream);
                        }
                    }
                    else if (isDeflate)
                    {
                        using (var gzipStream = new DeflateStream(await request.Content.ReadAsStreamAsync(),
                                                                  CompressionMode.Decompress))
                        {
                            await gzipStream.CopyToAsync(decompressedStream);
                        }
                    }

                    decompressedStream.Seek(0, SeekOrigin.Begin);

                    var originContent = request.Content;
                    request.Content = new StreamContent(decompressedStream);

                    foreach (var header in originContent.Headers)
                    {
                        request.Content.Headers.Add(header.Key, header.Value);
                    }
                }
            }

            return(await base.SendAsync(request, cancellationToken));
        }
Пример #21
0
 /// <summary>
 /// takes byte array and decompress it into Content
 /// </summary>
 public async Task Decompress()
 {
     if (CompressedContent != null && CompressedContent.Length != 0)
     {
         using (MemoryStream inStream = new MemoryStream(CompressedContent))
             using (MemoryStream outStream = new MemoryStream())
             {
                 using (GZipStream gz = new GZipStream(inStream, CompressionMode.Decompress))
                 {
                     await gz.CopyToAsync(outStream);
                 }
                 Content = Encoding.Unicode.GetString(outStream.ToArray());
             }
     }
     else
     {
         Content = "";
     }
 }
        public async Task DecompressAsync(string source, string destination)
        {
            using (var dt = _telemetryClient.StartOperation <DependencyTelemetry>("DotNetDecompression"))
                using (var pm = new PerfMon())
                    using (FileStream originalFileStream = File.OpenRead(source))
                        using (FileStream decompressedFileStream = File.Create(destination))
                            using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                            {
                                await decompressionStream.CopyToAsync(decompressedFileStream);

                                pm.Stop();
                                dt.Telemetry.Type = "DotNetDecompression";
                                dt.Telemetry.Properties.Add("source", source);
                                dt.Telemetry.Properties.Add("destination", destination);
                                dt.Telemetry.Properties.Add("run", _config.RunID);
                                dt.Telemetry.Properties.Add("used_cpu", pm.UsedProcessorTime.Ticks.ToString());
                                dt.Telemetry.Properties.Add("used_memory", pm.UsedMemory.ToString());
                            }
        }
Пример #23
0
        public static async Task <string> ReadAndDecodeAsStringAsync(this HttpContent content)
        {
            var autoDecode      = GetDecodeResponse();
            var contentEncoding = content.Headers.ContentEncoding;

            if (autoDecode)
            {
                if (contentEncoding.Any(enc => string.Equals("gzip", enc, StringComparison.OrdinalIgnoreCase)))
                {
                    var stream = new GZipStream(await content.ReadAsStreamAsync(), CompressionMode.Decompress);
                    using (var memory = new MemoryStream())
                    {
                        await stream.CopyToAsync(memory);

                        await memory.FlushAsync();

                        return(Encoding.UTF8.GetString(memory.ToArray()));
                    }
                }
                else if (contentEncoding.Any(enc => string.Equals("br", enc, StringComparison.OrdinalIgnoreCase)) ||
                         contentEncoding.Any(enc => string.Equals("brotli", enc, StringComparison.OrdinalIgnoreCase)))
                {
                    var stream = new BrotliStream(await content.ReadAsStreamAsync(), CompressionMode.Decompress);
                    using (var memory = new MemoryStream())
                    {
                        await stream.CopyToAsync(memory);

                        await memory.FlushAsync();

                        return(Encoding.UTF8.GetString(memory.ToArray()));
                    }
                }
            }

            if (contentEncoding.Any())
            {
                Console.WriteLine("[ Content is {0} encoding.  Set ARMCLIENT_DECODE_RESPONSE=1 env to automatically decode the content ]", string.Join(", ", contentEncoding));
                Console.WriteLine();
            }

            return(await content.ReadAsStringAsync());
        }
Пример #24
0
        public async Task <IActionResult> PostIngest()
        {
            var user     = Request.Headers[Consts.MetricsKeyHeader].First();
            var use_gzip = Request.Headers[Consts.CompressedBodyHeader].Any();

            _logger.Log(LogLevel.Information, $"Ingesting Modlist Definition for {user}");

            var modlistBytes = await Request.Body.ReadAllAsync();



            _logger.LogInformation("Spawning ingestion task");
            var tsk = Task.Run(async() =>
            {
                try
                {
                    if (use_gzip)
                    {
                        await using var os         = new MemoryStream();
                        await using var gZipStream =
                                        new GZipStream(new MemoryStream(modlistBytes), CompressionMode.Decompress);
                        await gZipStream.CopyToAsync(os);
                        modlistBytes = os.ToArray();
                    }

                    var modlist = new MemoryStream(modlistBytes).FromJson <ModList>();

                    var file = AbsolutePath.EntryPoint.Combine("mod_list_definitions")
                               .Combine($"{user}_{DateTime.UtcNow.ToFileTimeUtc()}.json");
                    file.Parent.CreateDirectory();
                    await using var stream = await file.Create();
                    modlist.ToJson(stream);
                    _logger.Log(LogLevel.Information, $"Done Ingesting Modlist Definition for {user}");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error ingesting uploaded modlist");
                }
            });

            return(Accepted(0));
        }
Пример #25
0
        public static async Task <byte[]> DecompressAsync(byte[] data)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentNullException("the data cannot be null", nameof(data));
            }

            using (var compressedDataStream = new MemoryStream(data))
            {
                using (var gzipStream = new GZipStream(compressedDataStream, CompressionMode.Decompress))
                {
                    using (var decompressedDataStream = new MemoryStream())
                    {
                        await gzipStream.CopyToAsync(decompressedDataStream);

                        return(decompressedDataStream.ToArray());
                    }
                }
            }
        }
Пример #26
0
        // Making this async since regular read/write are tested below
        private static async Task DecompressAsync(MemoryStream compareStream, MemoryStream gzStream)
        {
            var ms  = new MemoryStream();
            var zip = new GZipStream(gzStream, CompressionMode.Decompress);

            var GZipStream = new MemoryStream();
            await zip.CopyToAsync(GZipStream);

            GZipStream.Position    = 0;
            compareStream.Position = 0;

            byte[] compareArray = compareStream.ToArray();
            byte[] writtenArray = GZipStream.ToArray();

            Assert.Equal(compareArray.Length, writtenArray.Length);
            for (int i = 0; i < compareArray.Length; i++)
            {
                Assert.Equal(compareArray[i], writtenArray[i]);
            }
        }
Пример #27
0
        public static async Task <byte[]> DecompressAsync(byte[] input)
        {
            using var inmem  = new MemoryStream(input);
            using var output = new MemoryStream();
            using var gz     = new GZipStream(inmem, CompressionMode.Decompress);

            try
            {
                await gz.CopyToAsync(output);
            }
            catch (InvalidDataException invalidDataEx)
            {
                if (invalidDataEx.Message == "The archive entry was compressed using an unsupported compression method.")
                {
                    throw new UnsupportedCompressionAlgorithmException();
                }
            }

            return(output.ToArray());
        }
        /// <summary>
        /// Uploads a decompressed blob.
        /// </summary>
        /// <param name="srcBlob"></param>
        /// <param name="tgtContainer"></param>
        /// <param name="tgtBlobName"></param>
        /// <param name="cToken"></param>
        /// <returns></returns>
        private async Task UploadDecompressedSrcBlobToTgtBlobAsnyc(
            CloudBlob srcBlob,
            CloudBlobContainer tgtContainer,
            string tgtBlobName,
            CancellationToken cToken = default)
        {
            using (Stream blobStream = await srcBlob.OpenReadAsync(null, _options, null, cToken))
                using (var compressedBloblStream = new GZipStream(blobStream, CompressionMode.Decompress, leaveOpen: true))
                    using (MemoryStream streamToUplaod = new MemoryStream())
                    {
                        await compressedBloblStream.CopyToAsync(streamToUplaod).ConfigureAwait(false);

                        compressedBloblStream.Close();
                        streamToUplaod.Seek(0, SeekOrigin.Begin);
                        var targetBlob = tgtContainer.GetBlockBlobReference(tgtBlobName);
                        // set the content type of the original blob
                        targetBlob.Properties.ContentType = srcBlob.Metadata["contenttype"].FromBase64();
                        await targetBlob.UploadFromStreamAsync(streamToUplaod, null, _options, null, cToken).ConfigureAwait(false);
                    }
        }
Пример #29
0
        public static async Task <string> DecompressAsync(FileInfo fileToDecompress, string newFileName = null)
        {
            var currentFileName = fileToDecompress.FullName;

            if (string.IsNullOrEmpty(newFileName))
            {
                newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
            }

            using (var originalFileStream = fileToDecompress.OpenRead())
                using (var decompressedFileStream = File.Create(newFileName))
                    using (var decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                    {
                        await decompressionStream.CopyToAsync(decompressedFileStream);

                        //Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                    }

            return(newFileName);
        }
Пример #30
0
        /// <summary>
        /// Returns a new <c>MemoryStream</c> that has decompressed data inside. The provided stream is optionally closed.
        /// </summary>
        /// <param name="compressedStream"></param>
        /// <param name="leaveStreamOpen"></param>
        /// <returns></returns>
        public async ValueTask <MemoryStream> DecompressAsync(Stream compressedStream, bool leaveStreamOpen = false)
        {
            Guard.AgainstNullOrEmpty(compressedStream, nameof(compressedStream));

            if (compressedStream.Position == compressedStream.Length)
            {
                compressedStream.Seek(0, SeekOrigin.Begin);
            }

            var uncompressedStream = RecyclableManager.GetStream(nameof(RecyclableGzipProvider), CompressionHelpers.GetGzipUncompressedLength(compressedStream));

            using (var gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress, leaveStreamOpen))
            {
                await gzipStream
                .CopyToAsync(uncompressedStream)
                .ConfigureAwait(false);
            }

            return(uncompressedStream);
        }
Пример #31
0
        // Making this async since regular read/write are tested below
        private static async Task DecompressAsync(MemoryStream compareStream, MemoryStream gzStream)
        {
            var ms = new MemoryStream();
            var zip = new GZipStream(gzStream, CompressionMode.Decompress);

            var GZipStream = new MemoryStream();
            await zip.CopyToAsync(GZipStream);

            GZipStream.Position = 0;
            compareStream.Position = 0;

            byte[] compareArray = compareStream.ToArray();
            byte[] writtenArray = GZipStream.ToArray();

            Assert.Equal(compareArray.Length, writtenArray.Length);
            for (int i = 0; i < compareArray.Length; i++)
            {
                Assert.Equal(compareArray[i], writtenArray[i]);
            }
        }
Пример #32
0
 public void CopyToAsyncArgumentValidation()
 {
     using (GZipStream gs = new GZipStream(new MemoryStream(), CompressionMode.Decompress))
     {
         Assert.Throws<ArgumentNullException>("destination", () => { gs.CopyToAsync(null); });
         Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => { gs.CopyToAsync(new MemoryStream(), 0); });
         Assert.Throws<NotSupportedException>(() => { gs.CopyToAsync(new MemoryStream(new byte[1], writable: false)); });
         gs.Dispose();
         Assert.Throws<ObjectDisposedException>(() => { gs.CopyToAsync(new MemoryStream()); });
     }
     using (GZipStream gs = new GZipStream(new MemoryStream(), CompressionMode.Compress))
     {
         Assert.Throws<NotSupportedException>(() => { gs.CopyToAsync(new MemoryStream()); });
     }
 }