Beispiel #1
0
        public void LzmaEncodeDecodeTest()
        {
            using (var output = new FileStream(TemporaryFile, FileMode.Create))
            {
                var encoder = new LzmaEncodeStream(output);
                using (var inputSample = new FileStream(@"TestData\zip.zip", FileMode.Open))
                {
                    int bufSize = 24576, count;
                    var buf = new byte[bufSize];

                    while ((count = inputSample.Read(buf, 0, bufSize)) > 0)
                    {
                        encoder.Write(buf, 0, count);
                    }
                }

                encoder.Close();
            }

            var newZip = Path.Combine(OutputDirectory, "new.zip");

            using (var input = new FileStream(TemporaryFile, FileMode.Open))
            {
                var decoder = new LzmaDecodeStream(input);
                using (var output = new FileStream(newZip, FileMode.Create))
                {
                    int bufSize = 24576, count;
                    var buf = new byte[bufSize];

                    while ((count = decoder.Read(buf, 0, bufSize)) > 0)
                    {
                        output.Write(buf, 0, count);
                    }
                }
            }

            Assert.IsTrue(File.Exists(newZip));

            using (var extractor = new SevenZipExtractor(newZip))
            {
                Assert.AreEqual(1, extractor.FilesCount);
                Assert.AreEqual("zip.txt", extractor.ArchiveFileNames[0]);
            }
        }
Beispiel #2
0
 /// <summary>
 /// 使用Lzma算法压缩集合至字节流
 /// </summary>
 /// <typeparam name="Data"></typeparam>
 /// <param name="datas"></param>
 /// <returns></returns>
 public static byte[] Encodes <Data>(List <Data> datas)
 {
     if (datas == null || datas.Count == 0)
     {
         return(null);
     }
     byte[] buffers = null;
     using (MemoryStream output = new MemoryStream())
     {
         var encoder = new LzmaEncodeStream(output);
         using (var inputSample = new MemoryStream())
         {
             BinaryFormatter formatter = new BinaryFormatter();
             datas.ForEach(D =>
             {
                 try
                 {
                     formatter.Serialize(inputSample, D);
                 }
                 catch (SerializationException ex)
                 {
                     LogHelper.GetInstance().Write("对象" + D + "未能序列化!");
                 }
             }
                           );
             inputSample.Position = 0;
             int    bufSize = 24576, count;
             byte[] buf = new byte[bufSize];
             while ((count = inputSample.Read(buf, 0, bufSize)) > 0)
             {
                 encoder.Write(buf, 0, count);
             }
         }
         encoder.Close();
         buffers         = new byte[output.Length];
         output.Position = 0;
         output.Read(buffers, 0, buffers.Length);
     }
     return(buffers);
 }
        private void StartLogUpload(bool isPreviousCrashLog = false)
        {
            UploadingLog = true;
            TopText      = M3L.GetString(M3L.string_collectingLogInformation);
            NamedBackgroundWorker bw = new NamedBackgroundWorker(@"LogUpload");

            bw.DoWork += (a, b) =>
            {
                string logUploadText = LogCollector.CollectLogs(SelectedLog.filepath);
                using (var output = new MemoryStream())
                {
                    var encoder = new LzmaEncodeStream(output);
                    using (var normalBytes = new MemoryStream(Encoding.UTF8.GetBytes(logUploadText)))
                    {
                        int bufSize = 24576, count;
                        var buf = new byte[bufSize];

                        while ((count = normalBytes.Read(buf, 0, bufSize)) > 0)
                        {
                            encoder.Write(buf, 0, count);
                        }
                    }

                    encoder.Close();

                    //Upload log to ME3Tweaks

                    var lzmalog = output.ToArray();
                    try
                    {
                        //this doesn't need to technically be async, but library doesn't have non-async method.
                        string responseString = @"https://me3tweaks.com/modmanager/logservice/logupload.php".PostUrlEncodedAsync(new { LogData = Convert.ToBase64String(lzmalog), ModManagerVersion = App.BuildNumber, CrashLog = isPreviousCrashLog }).ReceiveString().Result;
                        Uri    uriResult;
                        bool   result = Uri.TryCreate(responseString, UriKind.Absolute, out uriResult) &&
                                        (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
                        if (result)
                        {
                            //should be valid URL.
                            //diagnosticsWorker.ReportProgress(0, new ThreadCommand(SET_DIAGTASK_ICON_GREEN, Image_Upload));
                            //e.Result = responseString;
                            Log.Information(@"Result from server for log upload: " + responseString);
                            b.Result = responseString;
                            return;
                        }
                        else
                        {
                            Log.Error(@"Error uploading log. The server responded with: " + responseString);
                            b.Result = M3L.GetString(M3L.string_interp_serverRejectedTheUpload, responseString);
                        }
                    }
                    catch (AggregateException e)
                    {
                        Exception ex        = e.InnerException;
                        string    exmessage = ex.Message;
                        b.Result = M3L.GetString(M3L.string_interp_logWasUnableToUpload, exmessage);
                    }
                    catch (FlurlHttpTimeoutException)
                    {
                        // FlurlHttpTimeoutException derives from FlurlHttpException; catch here only
                        // if you want to handle timeouts as a special case
                        Log.Error(@"Request timed out while uploading log.");
                        b.Result = M3L.GetString(M3L.string_interp_requestTimedOutUploading);
                    }
                    catch (Exception ex)
                    {
                        // ex.Message contains rich details, inclulding the URL, verb, response status,
                        // and request and response bodies (if available)
                        Log.Error(@"Handled error uploading log: " + App.FlattenException(ex));
                        string exmessage = ex.Message;
                        var    index     = exmessage.IndexOf(@"Request body:");
                        if (index > 0)
                        {
                            exmessage = exmessage.Substring(0, index);
                        }

                        b.Result = M3L.GetString(M3L.string_interp_logWasUnableToUpload, exmessage);
                    }
                }
            };
            bw.RunWorkerCompleted += (a, b) =>
            {
                if (b.Result is string response)
                {
                    if (response.StartsWith(@"http"))
                    {
                        Utilities.OpenWebpage(response);
                    }
                    else
                    {
                        OnClosing(DataEventArgs.Empty);
                        var res = M3L.ShowDialog(Window.GetWindow(this), response, M3L.GetString(M3L.string_logUploadFailed), MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }
                OnClosing(DataEventArgs.Empty);
            };
            bw.RunWorkerAsync();
        }