0

这是此处的后续问题:Decompress Stream to String using SevenZipSharp

下面的代码在它接受一个字符串并成功压缩和解压缩它的意义上起作用。

using System;
using System.IO;
using SevenZip;

namespace _7ZipWrapper
{
    public class ProgramOriginal
    {
        public static void Main()
        // This should be broken into separate methods
        {
            // Setup Input String
            var strToCompress = "This String"; // will pass as parameter
            var memStreamToCompress = new MemoryStream();

            var StringToStream = new StreamWriter(memStreamToCompress);
            StringToStream.Write(strToCompress);
            StringToStream.Flush();

            // Confirm the Input Stream is As-Expected
            memStreamToCompress.Position = 0;
            var MemoryAsString = new StreamReader(memStreamToCompress);
            Console.WriteLine("Stream in memory: " + MemoryAsString.ReadToEnd());
            Console.ReadKey();

            // Setup the SevenZip Dll
            SevenZipCompressor.SetLibraryPath(@"C:\Temp\7za64.dll");

            // Set up a compressor...
            SevenZipCompressor SevenZipC = new SevenZipCompressor();
            SevenZipC.CompressionMethod = CompressionMethod.Ppmd;
            SevenZipC.CompressionLevel = global::SevenZip.CompressionLevel.Ultra;
            SevenZipC.ScanOnlyWritable = true;

            // Compress PassedStream -> CompressedStream
            var compressedMemoryStream = new MemoryStream();
            SevenZipC.CompressStream(memStreamToCompress, compressedMemoryStream, "Optional Password Field");
            compressedMemoryStream.Position = 0;
            StreamReader compressedMemoryAsString = new StreamReader(compressedMemoryStream);

            // Show that we have a compressed String
            compressedMemoryStream.Position = 0;
            Console.WriteLine(compressedMemoryAsString.ReadToEnd());
            Console.ReadKey();

            // Set up a decompressor... (only needs to know what to decompress)
            compressedMemoryStream.Position = 0;
            var SevenZipE = new SevenZip.SevenZipExtractor(compressedMemoryStream, "Optional Password Field");

            // Decompress CompressedStream
            var DecompressedMemoryStream = new MemoryStream();
            SevenZipE.ExtractFile(0, DecompressedMemoryStream);

            // Show that DecompressedMemoryStream is valid
            DecompressedMemoryStream.Position = 0;
            StreamReader decompressedStreamAsText = new StreamReader(DecompressedMemoryStream);
            Console.WriteLine("Decompressed String: " + decompressedStreamAsText.ReadToEnd());
            Console.ReadKey();
        }
    }
}

但是,上面的代码在当前形式下显然没有什么好处(这注定要成为一个 COM DLL)

我以为我是家常便饭,重构将是小菜一碟,但是,我尝试将代码重新排列成有用的东西让我不知道为什么以下代码会抛出 System.ArgumentException ('The stream is无效或未找到相应的签名。')但上面的代码运行没有问题。

从根本上说,肯定存在某种差异,但我不知道是什么原因造成的,以及如何解决。一个附有简短解释的解决方案将不胜感激。

using System;
using System.IO;
using SevenZip;

namespace _7ZipWrapper
{
    public class Program
    {
        public static void Main()
        {
            // Setup Input String
            var strToCompress = "This String"; // will eventually pass as parameter


            // Convert input string to memory stream
            var memStreamToCompress = StringToStream(strToCompress);


            // Confirm the Input Stream is As-Expected
            memStreamToCompress.Position = 0;
            var MemoryAsString = new StreamReader(memStreamToCompress);
            Console.WriteLine("Stream in memory: " + MemoryAsString.ReadToEnd());
            Console.ReadKey();

            // Compress the Stream
            memStreamToCompress.Position = 0;
            var compressedString = StreamCompress(memStreamToCompress, "password");

            // Decompress the String
            var memStreamToRestore = StringToStream(compressedString);
            memStreamToRestore.Position = 0;

            var decompressedString = StreamDecompress(memStreamToRestore, "password");

            Console.WriteLine(decompressedString);
            Console.ReadKey();
        }

        private static MemoryStream StringToStream(string strToMemoryStream)
        {
            var memoryStream = new MemoryStream();
            var writer = new StreamWriter(memoryStream);
            writer.Write(strToMemoryStream);
            writer.Flush();
            return memoryStream;
        }

        private static MemoryStream StringToStream1(string strToMemoryStream)
        {
            var memoryStream = new MemoryStream();
            var writer = new StreamWriter(memoryStream);
            writer.Write(strToMemoryStream);
            writer.Flush();
            return memoryStream;
        }


        private static string StreamCompress(MemoryStream memStreamToCompress, string optionalPassword)
        {
            // Setup the SevenZip Dll
            SevenZipCompressor.SetLibraryPath(@"C:\Temp\7za64.dll");

            // Set up a compressor...
            SevenZipCompressor SevenZip = new SevenZipCompressor();
            SevenZip.CompressionMethod = CompressionMethod.Ppmd;
            SevenZip.CompressionLevel = global::SevenZip.CompressionLevel.Ultra;
            SevenZip.ScanOnlyWritable = true;

            // Compress PassedStream -> CompressedStream
            var compressedMemoryStream = new MemoryStream();
            SevenZip.CompressStream(memStreamToCompress, compressedMemoryStream, optionalPassword); // "Optional Password Field"
            compressedMemoryStream.Position = 0;
            StreamReader compressedMemoryAsString = new StreamReader(compressedMemoryStream);
            return compressedMemoryAsString.ReadToEnd();
        }

        private static string StreamDecompress(MemoryStream compressedMemoryStream, string optionalPassword)
        {
            // Setup the SevenZip Dll
            SevenZipExtractor.SetLibraryPath(@"C:\Temp\7za64.dll");

            // Set up a decompressor... (only needs to know what to decompress)
            compressedMemoryStream.Position = 0;
            // CRASHES Next Line: System.ArgumentException: 'The stream is invalid or no corresponding signature was found.'
            var SevenZip = new SevenZip.SevenZipExtractor(compressedMemoryStream, optionalPassword);  

            // Decompress CompressedStream
            var DecompressedMemoryStream = new MemoryStream();
            SevenZip.ExtractFile(0, DecompressedMemoryStream);

            // Show that DecompressedMemoryStream is valid
            DecompressedMemoryStream.Position = 0;
            StreamReader decompressedStreamAsText = new StreamReader(DecompressedMemoryStream);
            return decompressedStreamAsText.ReadToEnd();
        }
    }
}

注意:虽然我很欣赏这段代码可能存在多个问题,但我正在学习并打算在 CodeReview 上进行下一次迭代。也就是说,请随时提供建议,但现在这对我来说是一个学习练习。TIA

4

2 回答 2

1

您的工作代码已解压缩compressedMemoryStream。您损坏的代码解压缩了一个compressedMemoryStream.

正如我在上一个问题中所说,压缩的结果不是 text。如果必须将其表示为文本,则应使用 Base64 或十六进制。但是仅仅从它读取就好像它是 UTF-8 编码的文本(这就是你现在正在做的)根本行不通。

您的StreamCompress方法的结果可能应该是byte[]. 这很容易实现:

// Note: changed input type to just Stream to make it more general
private static byte[] StreamCompress(Stream input, string optionalPassword)
{
    SevenZipCompressor.SetLibraryPath(@"C:\Temp\7za64.dll");
    SevenZipCompressor SevenZip = new SevenZipCompressor();
    SevenZip.CompressionMethod = CompressionMethod.Ppmd;
    SevenZip.CompressionLevel = global::SevenZip.CompressionLevel.Ultra;
    SevenZip.ScanOnlyWritable = true;

    var output = new MemoryStream();
    SevenZip.CompressStream(input, output, optionalPassword);
    // You don't even need to rewind when you call ToArray
    return output.ToArray();
}

当您想解压缩时,您可以创建一个MemoryStream来包装该字节数组,作为众多选项之一。

如果您确实需要将结果作为字符串,则可以调用Convert.ToBase64String,然后再调用Convert.FromBase64String以取回原始字节。与您当前的方法不同,这不会丢失信息。

我还应该指出,除非您想要特定于 7zip 的压缩,否则也有大量纯托管压缩库可用。

于 2018-05-20T06:31:02.157 回答
0

C# 使用 SevenZipSharp 压缩和解压缩字符串

基本问题是我没有正确使用字符串,将内存流转换为字符串不起作用。解决方案使用base64编码使压缩字符串可移植;这使它能够存储在适合我需要的 XML / JSON 文件中。谢谢@Daisy Shipton(见:这个答案)。

来自 VBA 的构造函数的使用(在newing时传递参数)并不是很明显,但这有助于. 这是关键:

// Create a memory stream from the input: base64 --> bytes --> memStream
Byte[] compBytes = Convert.FromBase64String(input);
MemoryStream compStreamIn = new MemoryStream(compBytes);

希望这对其他人有帮助:

using System;
using System.IO;
using System.Text;
using SevenZip;

namespace _7ZipWrapper
{
    public class ProgramToModify
    {
        public static void Main()
        {
            var input = "Some string"; // Input String will pass as parameter

            var compressed = MyEncode(input);
            Console.WriteLine("Compressed String: " + compressed);

            var decodedString = myDecode(compressed);
            Console.WriteLine("Decompressed String: " + decodedString);
            Console.ReadKey();
        }

        // Returns compressed and encoded base64 string from input 
        private static String MyEncode(string input) 
        {
            // Setup the SevenZip Dll
            SevenZipCompressor.SetLibraryPath(@"C:\Temp\7za64.dll");
            SevenZipCompressor SevenZipC = new SevenZipCompressor();
            SevenZipC.CompressionMethod = CompressionMethod.Ppmd;
            SevenZipC.CompressionLevel = global::SevenZip.CompressionLevel.Ultra;
            SevenZipC.ScanOnlyWritable = true;

            var memoryStream = new MemoryStream(); // Create Memory Stream
            var streamWriter = new StreamWriter(memoryStream);
            streamWriter.Write(input); // streamWriter writes input to memoryStream
            streamWriter.Flush();

            // Compress: memoryStream -> cmpdMemoryStream
            var cmpdMemoryStream = new MemoryStream();
            SevenZipC.CompressStream(memoryStream, cmpdMemoryStream, "Optional Password Field");

            Byte[] bytes = cmpdMemoryStream.ToArray();
            return Convert.ToBase64String(bytes);
        }


        // Returns plain string from compressed and encoded input
        private static String myDecode(string input) 
        {
            // Create a memory stream from the input: base64 --> bytes --> memStream
            Byte[] compBytes = Convert.FromBase64String(input);
            MemoryStream compStreamIn = new MemoryStream(compBytes);

            SevenZipExtractor.SetLibraryPath(@"C:\Temp\7za64.dll");
            var SevenZipE = new SevenZip.SevenZipExtractor(compStreamIn, "Optional Password Field");

            var OutputStream = new MemoryStream();
            SevenZipE.ExtractFile(0, OutputStream);

            var OutputBase64 = Convert.ToBase64String(OutputStream.ToArray());
            Byte[] OutputBytes = Convert.FromBase64String(OutputBase64);
            string output = Encoding.UTF8.GetString(OutputBytes);
            return output;
        }
    }
}
于 2018-05-21T01:20:41.183 回答