C#: Programmatically create a split zip file

3.2k views Asked by At

I want to create a zip file from a folder that it around 1.5GB, and have that zip file be split into chunks of 100MB. I've found quite a few threads on this, but nothing has quite worked out for me.

First I tried System.IO.Compression, but found that it doesn't support splitting the zip files (please correct me if I'm wrong!).

Next I tried Ionic.zip, which looked super easy, but every set of files I create is corrupted in some way (for example, the following code that uses the fonts directory as a test directory creates a set of files that I can't then open or unzip as an archive with either winzip or winrar):

using (var zipFile = new Ionic.Zip.ZipFile(Encoding.UTF8))
{
    zipFile.AddDirectory("c:\\windows\\fonts", directoryPathInArchive: string.Empty);
    zipFile.MaxOutputSegmentSize = 100 * 1000000;
    zipFile.Save("c:\\users\\me\\test.zip");
}

Finally, I've tried the 7z.dll and SharpCompress. Using the Command Line and the 7z.exe file, the following works perfectly:

7z.exe a "c:\users\me\test.zip" "c:\Windows\Fonts" -v100m

But the following code gives the error "Value does not fall within the expected range."

SevenZipCompressor.SetLibraryPath("c:\\program files\\7-zip\\7z.dll");
var compressor = new SevenZipCompressor();
compressor.CompressionMethod = CompressionMethod.Lzma2;
compressor.CustomParameters.Add("v", "100m");
compressor.CompressDirectory("c:\\windows\\fonts\\", "c:\\users\\me\\test.zip");

I've also tried the following (having tried to figure out how the command line switches work in SharpCompress) which does create a zip file, but doesn't split it up:

SevenZipCompressor.SetLibraryPath("c:\\program files\\7-zip\\7z.dll");
var compressor = new SevenZipCompressor();
compressor.CompressionMethod = CompressionMethod.Lzma2;
compressor.CustomParameters.Add("mt", "on");
compressor.CustomParameters.Add("0", "LZMA2:c=100m");
compressor.CompressDirectory("c:\\windows\\fonts\\", "c:\\users\\me\\test.zip");

Does anyone know why any of the above methods aren't working? Or are there any other ways that people have got working that I haven't tried yet?

Thanks!

2

There are 2 answers

0
Mark Adler On

I am not aware of a library that supports the PKZIP split zip file format.

0
KarMau On

It's an old question, but Ionic is working. Maybe a little bit tricky, but ok. My first version also creates a set of files that I can't then unzip. But after changing the order of commands, the output can be unzipped.

    private static void CreateEncryptedZipFile(string filename, string to, FileInfo fi, string password)
    {
        using (var zipFile = new Ionic.Zip.ZipFile())
        {
            zipFile.Password = password;
            zipFile.Encryption = Ionic.Zip.EncryptionAlgorithm.WinZipAes256;
            zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            zipFile.AddFile(filename, directoryPathInArchive: string.Empty);
            zipFile.MaxOutputSegmentSize = 1024*1024*128;
            zipFile.Save(to + ".zip");
        }
        createXMLInfo(fi, to);
    }