简体   繁体   English

将压缩的内存流保存到文件流,最终文件已损坏

[英]Saving compressed memory stream to filestream , endfile is corrupted

i am using seven.zip.sharp to compress a stream. 我正在使用seven.zip.sharp来压缩流。 I then want to after the compression is done, save the data in the memory stream to a filestream. 然后,我想在压缩完成后,将内存流中的数据保存到文件流中。 The file being a ".7z" file. 该文件是“ .7z”文件。

Problem: 问题:
The output file is corrupted and i am unable to decompress it manually. 输出文件已损坏,我无法手动将其解压缩。 Using notepad++ i am also unable to see the header that is normally found in a 7zip file. 使用记事本++,我也看不到通常在7zip文件中找到的标题。

Here is the code: 这是代码:

    //Memory stream used to store compressed stream
    public System.IO.Stream TheStream = new System.IO.MemoryStream();

    //Start compress stream
    private void button1_Click(object sender, EventArgs e)
    {
        Thread newThread1 = new Thread(this.COMP_STREAM);
        newThread1.Start();
    }

    //See size of stream on demand
    private void button2_Click(object sender, EventArgs e)
    {
            textBox1.Clear();
            textBox1.Text += "-" + TheStream.Length;
    }

    //To Create file
    private void button3_Click(object sender, EventArgs e)
    {


        byte[] buffer = new byte[1024]; // Change this to whatever you need

        using (System.IO.FileStream output = new FileStream(@"F:\Pasta desktop\sss\TESTEmiau.7z", FileMode.Create))
        {
            int readBytes = 0;
            while ((readBytes = TheStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, readBytes);
            }
            output.Close();
        }
        MessageBox.Show("DONE");
    }

    //To compress stream
    public void COMP_STREAM()
    {
        SevenZip.SevenZipCompressor.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll");
        var stream = System.IO.File.OpenRead(@"F:\Pasta desktop\sss\lel.exe");

        SevenZip.SevenZipCompressor compressor = new SevenZip.SevenZipCompressor();
        compressor.CompressionMethod = SevenZip.CompressionMethod.Lzma2;
        compressor.CompressionLevel = SevenZip.CompressionLevel.Ultra;
        compressor.CompressStream(stream, TheStream); //I know i can just use a FileStream here but i am doing this from testing only.
        MessageBox.Show("Done");
    }

Please someone alter the question to make it look better. 请有人更改问题以使其看起来更好。 And add better title to if u desire. 并根据需要添加更好的标题。 Thank you. 谢谢。

So you planned to store the compressed stream in a temporary MemoryBuffer, and later write it out to a file. 因此,您计划将压缩的流存储在临时的MemoryBuffer中,然后再将其写出到文件中。 The problem is that the MemoryStream must be reset after the write, so the read operation reads from the beginning. 问题在于在写操作之后必须重置MemoryStream,因此读操作将从头读取。 If your output file is of size 0, then I'm pretty sure that this is the problem. 如果您的输出文件的大小为0,那么我很确定这是问题所在。

Here is a fix: 解决方法:

// Seek the beginning of the `MemoryStrem` before writing it to a file:
TheStream.Seek(0, SeekOrigin.Begin);

Or you can declare the stream to be a MemoryStream , and use the Position property: 或者,您可以将流声明为MemoryStream ,并使用Position属性:

TheStream.Position = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM