使用 7z SDK 解压多个文件

我正在尝试实现 7z SDK 以一次压缩和解压缩多个文件。


我一次尝试使用一个文件,两种方法都有效,现在我对 compress 方法进行了一些更改,以支持压缩多个文件。


public static void CompressFile(List<string> inFiles, string outFile)

{

        SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder();


        FileStream output = new FileStream(outFile, FileMode.Create);


        // Write the encoder properties

        coder.WriteCoderProperties(output);


        int listSize = inFiles.Count;


        // get the size of my list to loop through all the items

        // Writing each file on the compressed file at a time


        for(int i = 0; i < listSize; i++)

        {

            FileStream input = new FileStream(inFiles[i], FileMode.Open);


            // Write the decompressed file size.

            output.Write(BitConverter.GetBytes(input.Length), 0, 8);


            // Encode the file.

            coder.Code(input, output, input.Length, -1, null);

        }

        output.Flush();

        output.Close();

    }

我按预期得到了一个压缩文件,但我需要实现解压缩方法来测试一切是否顺利。


我一直在弄清楚如何实现解压缩多个文件所需的更改:


public static void DecompressFile(string inFile, string outFile)

{

        //Original method used to decompress one file worked.


        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();



            FileStream input = new FileStream(inFile, FileMode.Open);

            FileStream output = new FileStream(outFile, FileMode.Create);


            // Read the decoder properties

            byte[] properties = new byte[5];

            input.Read(properties, 0, 5);


            // Read in the decompress file size.

            byte[] fileLengthBytes = new byte[8];

            input.Read(fileLengthBytes, 0, 8);

            long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);


            coder.SetDecoderProperties(properties);

            coder.Code(input, output, input.Length, fileLength, null);

            output.Flush();

            output.Close();


    }

我有一个想法(不知道好不好)使用循环结构压缩所有文件,但我不能解压缩它。哪种方法最适合解压缩应该包含多个文件的文件?


德玛西亚99
浏览 182回答 1
1回答

慕妹3146593

我用 gzipstream 做了类似的事情。但这完全取决于您希望如何打包文件。在这种情况下,这样的事情将是理想的。string payload = Path.GetTempFileName();using (FileStream temp_fs = new FileStream(payload, FileMode.OpenOrCreate)){&nbsp; &nbsp; using (BinaryWriter output_s = new BinaryWriter(new FileStream("target.out", FileMode.OpenOrCreate)))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Write a blank. must be a long!&nbsp; &nbsp; &nbsp; &nbsp; output_s.Write((long)0);&nbsp; &nbsp; &nbsp; &nbsp; foreach (string file in Files)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Write the files name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_s.Write(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long start = temp_fs.Position;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Write the starting point&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_s.Write(start);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Compress the file to the payload&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (GZipStream gzip = new GZipStream(temp_fs, CompressionMode.Compress, true))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (FileStream fs = new FileStream(file, FileMode.Open))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fs.CopyTo(gzip);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Write the length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_s.Write(temp_fs.Position - start);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //When all files are written&nbsp; &nbsp; &nbsp; &nbsp; //Get the size of our header&nbsp; &nbsp; &nbsp; &nbsp; long headersize = output_s.BaseStream.Length - 8;&nbsp; &nbsp; &nbsp; &nbsp; //Copy the temp file data to the end&nbsp; &nbsp; &nbsp; &nbsp; temp_fs.CopyTo(output_s.BaseStream);&nbsp; &nbsp; &nbsp; &nbsp; //Reset to the start of the stream&nbsp; &nbsp; &nbsp; &nbsp; output_s.BaseStream.Position = 0;&nbsp; &nbsp; &nbsp; &nbsp; //override our zero&nbsp; &nbsp; &nbsp; &nbsp; output_s.Write(headersize);&nbsp; &nbsp; }}File.Delete(payload);当您读取文件时,将进行二进制读取,您将能够按名称获取文件,然后在文件名之后的下一个很长的时间将是其大小并开始。然后你将节头+pos解压缩到长度。检索您的文件。
打开App,查看更多内容
随时随地看视频慕课网APP