1

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!

John Darvill
  • 1,274
  • 11
  • 17
  • Can't you simply call 7z.exe using `Process.Start()`? – Dan Wilson May 23 '18 at 15:54
  • 1
    Are you using this https://github.com/adamhathcock/sharpcompress? Under To Do that page states "Multi-volume Zip support". I assume it means that SharpCompress cannot do (yet) what you are trying to accomplish. – Christoph May 23 '18 at 15:59
  • 1
    What exactly do you mean by "split"? Do you want the resulting <= 100 MB files to each be a valid zip file with complete entries? Or do you just want to break the zip file into 100 MB pieces that need to be reassembled in order to extract files? – Mark Adler May 23 '18 at 16:38
  • @MarkAdler I want to get the same result as the command line gets with the "v" option. You get several files (all around the size you specify) with the .001, .002, .003 extensions. Then you can select one of them and extract the files from all at once. – John Darvill May 24 '18 at 08:47
  • @DanWilson Yeah I can just do that for now (nagging me that I can't do it more elegantly though!). Cheers! (tried to upvote your comment but couldn't for some reason, sorry) – John Darvill May 24 '18 at 08:50

2 Answers2

0

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

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
0

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);
    }
KarMau
  • 5
  • 3