简体   繁体   English

使用7z SDK解压缩多个文件

[英]Decompress multiple files using 7z SDK

I am trying to implement the 7z SDK to compress and decompress multiple files at once. 我正在尝试实施7z SDK,以一次压缩和解压缩多个文件。

I tried with one file at a time and both methods worked, now I implemented some changes on the compress method to support multiple files being compressed. 我一次尝试一个文件,并且两种方法都起作用,现在我对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();
    }

I got a compressed file as expected but I need to implement the decompress method to test if everything went well. 我得到了预期的压缩文件,但是我需要实现decompress方法来测试一切是否顺利。

I got stuck on figuring out how to implement the changes I need to decompress multiple files: 我被困在弄清楚如何实施我需要解压缩多个文件的更改:

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();

    }

I had the idea (dont know if good or not) to compress all the files using a loop structure, but to decompress it I cant. 我有一个想法(不知道是否好),使用循环结构压缩所有文件,但是我无法解压缩。 Which approach would be the ideal to decompress a file that is supposed to contain several files? 哪种方法最适合解压缩应该包含多个文件的文件?

I did something like this with gzipstream. 我用gzipstream做过这样的事情。 but its all about how you want pack the files. 但有关您要如何打包文件的全部内容。

In this case something like this would be ideal. 在这种情况下,这将是理想的。

string payload = Path.GetTempFileName();
using (FileStream temp_fs = new FileStream(payload, FileMode.OpenOrCreate))
{
    using (BinaryWriter output_s = new BinaryWriter(new FileStream("target.out", FileMode.OpenOrCreate)))
    {
        //Write a blank. must be a long!
        output_s.Write((long)0);
        foreach (string file in Files)
        {

            //Write the files name
            output_s.Write(file);
            long start = temp_fs.Position;

            //Write the starting point
            output_s.Write(start);

            //Compress the file to the payload
            using (GZipStream gzip = new GZipStream(temp_fs, CompressionMode.Compress, true))
            {
                using (FileStream fs = new FileStream(file, FileMode.Open))
                {
                    fs.CopyTo(gzip);
                }
            }

            //Write the length
            output_s.Write(temp_fs.Position - start);
        }

        //When all files are written
        //Get the size of our header
        long headersize = output_s.BaseStream.Length - 8;

        //Copy the temp file data to the end
        temp_fs.CopyTo(output_s.BaseStream);

        //Reset to the start of the stream
        output_s.BaseStream.Position = 0;

        //override our zero
        output_s.Write(headersize);
    }

}
File.Delete(payload);

When you read your file will a binary read you will be able to grab the file by name then the next to long after the filename will be its size and start. 当您读取文件时,将进行二进制读取,您将能够按名称抓取该文件,然后文件名后面的不久将是其大小并开始。 then you would decompress section header+pos to length. 那么您可以将节头+ pos解压缩为长度。 to retrieve your file. 检索您的文件。

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

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