解压缩拆分的zip文件c#(从CD加载部分)

时间:2017-03-15 07:36:19

标签: c# unzip dotnetzip

我试图解压缩分为5个部分的zip文件。问题是每个部分都在不同的CD中,我需要能够在解压缩时更换磁盘。 我已经使用Ionic.Zip来做到这一点,没有运气!

foreach (var entry in zip.Entries)
    {
        var stream = entry.OpenReader();
        var buffer = new byte[readByte];
        int n;
        try
        {
            while ((n = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
               fileStream.Write(buffer, 0, buffer.Length);
            }
        }
        catch (FileNotFoundException ex)
        {
            // stream is closed and you cant continue           
            MessageBox.Show("Change CD");

            while ((n = stream.Read(buffer,0, buffer.Length)) > 0)
            {
                fileStream.Write(buffer, 0, buffer.Length);
            }
        }
    }

我尝试过SevenZip,ZipSharp,但我无法实现它! 有没有办法处理这种情况?

1 个答案:

答案 0 :(得分:2)

要打开拆分的ZipArchive,不需要额外的库,但需要完成一些额外的工作

List<string> files = new List<string> { "zip.001",
                                        "zip.002",
                                        "zip.003",
                                        "zip.004",
                                        };
using (var zipFile = new ZipArchive(new CombinationStream(files.Select(x => new FileStream(x, FileMode.Open) as Stream).ToList()), ZipArchiveMode.Read)) 
{
    // Do whatever you want
}

在此示例中,我使用CombinationStream打开所有zip文件,但您可以轻松编写自己的类继承Stream以满足您的需求,等待读取所有CD。

起点可能是(只是伪代码):

public class MultiDeviceStream : Stream
{
    [...]
    private Queue<Stream> streams;
    private Stream activeStream;

    public byte ReadByte() {
        byte result;
        if (!activeStream.EndOfStream) {
            result = activeStream.ReadByte();
            if (!streams.CanDequeue && activeStream.EndOfStream) {
                // raise some event signaling to change the CD and wait for the new filestream here
                this.EndOfStream = true;
            }
        } else {
            if (streams.CanDequeue) {
                activeStream = streams.Dequeue();
            }
            else
            {
                throw EndOfStreamException();
            }
            return ReadByte();
        }
        return result;
    }
}