将ZipFile分成多个小块,然后再次组合块以创建ZipFile

时间:2018-07-12 16:23:36

标签: c# asp.net .net

我想将大型zip文件传输到远程服务器。为此,请将zip文件拆分为小块,然后将这些块发送到远程服务器。 当再次组合所有这些块并在提取期间,其显示的ZipFile已损坏。 大型文本和图像文件可以正确拆分和合并。只有Zip文件有问题。

代码:

public static void SplitFile(string inputFile, int chunkSize, string path)
{
    const int BUFFER_SIZE = 20 * 1024;
    byte[] buffer = new byte[BUFFER_SIZE];

    using (Stream input = File.OpenRead(inputFile))
    {
        int index = 0;
        while (input.Position < input.Length)
        {
            using (Stream output = File.Create(path + "\\" + index))
            {
                int remaining = chunkSize, bytesRead;
                while (remaining > 0 && (bytesRead = input.Read(buffer, 0,
                        Math.Min(remaining, BUFFER_SIZE))) > 0)
                {
                    output.Write(buffer, 0, bytesRead);
                    remaining -= bytesRead;
                }
            }
            index++;                
        }
    }
}

public static void Combine(string directoryPath, string fullFileName) {
    if(File.Exists(fullFileName)) {
        File.Delete(fullFileName);
    }
    using(Stream output = File.Create(fullFileName)) {
        foreach(string file in Directory.EnumerateFiles(directoryPath)) {
            using(Stream input = File.OpenRead(file)) {
                input.CopyTo(output);
            }
        }
    }
}    

1 个答案:

答案 0 :(得分:1)

下面的代码解决了 ZIP 文件问题,我认为它与 zip 文件没有特别的关系,我认为合并没有按照正确的顺序将文件重新组合在一起。

添加一个更好的文件名,然后在组合中添加一个“OrderBy”似乎已经解决了这个问题。

public static void SplitFile(string inputFile, int chunkSize, string path)
    {
        const int BUFFER_SIZE = 20 * 1024;
        byte[] buffer = new byte[BUFFER_SIZE];

        using (Stream input = File.OpenRead(inputFile))
        {
            int index = 0;
            while (input.Position < input.Length)
            {
                using (Stream output = File.Create(Path.Combine(path, index.ToString().PadLeft(15, '0'))))
                {
                    int remaining = chunkSize, bytesRead;
                    while (remaining > 0 && (bytesRead = input.Read(buffer, 0,
                            Math.Min(remaining, BUFFER_SIZE))) > 0)
                    {
                        output.Write(buffer, 0, bytesRead);
                        remaining -= bytesRead;
                    }
                }
                index++;
            }
        }
    }

    public static void Combine(string directoryPath, string fullFileName)
    {
        if (File.Exists(fullFileName))
            File.Delete(fullFileName);

        using (Stream output = File.Create(fullFileName))
            foreach (string file in Directory.GetFiles(directoryPath).OrderBy(x => x))
            {
                Console.WriteLine(file);
                using (Stream input = File.OpenRead(file))
                    input.CopyTo(output);
            }
                
        
        foreach (string file in Directory.EnumerateFiles(directoryPath))
            System.IO.File.Delete(file);
    }