簡體   English   中英

使用7z SDK解壓縮多個文件

[英]Decompress multiple files using 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();
    }

我得到了預期的壓縮文件,但是我需要實現decompress方法來測試一切是否順利。

我被困在弄清楚如何實施我需要解壓縮多個文件的更改:

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

    }

我有一個想法(不知道是否好),使用循環結構壓縮所有文件,但是我無法解壓縮。 哪種方法最適合解壓縮應該包含多個文件的文件?

我用gzipstream做過這樣的事情。 但有關您要如何打包文件的全部內容。

在這種情況下,這將是理想的。

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

當您讀取文件時,將進行二進制讀取,您將能夠按名稱抓取該文件,然后文件名后面的不久將是其大小並開始。 那么您可以將節頭+ pos解壓縮為長度。 檢索您的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM