Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
i am looking for some example which can help me to understand how can i zip files in C# in parts i.e. if i have Folder Size = 20MB , I want to Compress It As split the archive in 4 parts of 5MB each. when i have those 4 files, I can extract them to the original files. So each individual part may not be a proper zip but all 4 parts should extract properly

Could someone suggest? thanks,


C#
using (var memoryStream = new MemoryStream())
{
 using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
 {
  var demoFile = archive.CreateEntry("foo.txt");

 }

 using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
   {
     memoryStream.Position=0;
     memoryStream.WriteTo(fileStream);
   }
 }

Iam Using System.IO.Compression and some time Foo.txt Is More than 3 Giga so i need to split this file to multiple compressed file

Thanks
Posted
Updated 8-Sep-15 1:56am
v3
Comments
Maciej Los 8-Sep-15 7:45am    
What have you tried till now? Where are you stuck? What kind of library do you use to zip folders/files?

1 solution

Have a look here: System.IO.Packaging Namespace[^], which provides classes that support storage of multiple data objects in a single container.
Usage: Introducing System.IO.Packaging[^]


Another option:
DotNetZip is a FAST, FREE class library and toolset for manipulating zip files[^]
Usage: Create ZIP Files From An ASP.NET Application[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Sep-15 9:20am    
5ed, but...

I'm not sure that breaking the compressed file into separate pieces of fixed size is available on those APIs; it should be checked up. Do you know if this feature is there? Development of such code in the user's side is somewhat problematic.

—SA
Member 11593359 8-Sep-15 9:55am    
Thank you for your reply
There is already library Do The require Ionic.Zip
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(sFolderPath);
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
zip.MaxOutputSegmentSize = 10000 * 1024; // 100k segments
zip.Save(sbZipFolderName.ToString());
SegmentsCreated = zip.NumberOfSegmentsForMostRecentSave;
}
but i need to remove this library from my project and Use System.IO.Compression
Thanks
Maciej Los 8-Sep-15 10:15am    
You're very welcome ;)
Maciej Los 8-Sep-15 10:15am    
Thank you, Sergey.
Wendelius 8-Sep-15 12:12pm    
That would do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900