Created
July 14, 2023 02:42
-
-
Save conholdate-gists/0ff227f36d7d32e73544e6e25f22396d to your computer and use it in GitHub Desktop.
Create ZIP in C# | Compress Files Folders into ZIP Archive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (FileStream zipFile = File.Open(dataDir + "CompressSingleFile_out.zip", FileMode.Create)) | |
{ | |
//File to be added to archive | |
using (FileStream source1 = File.Open(dataDir + "test.txt", FileMode.Open, FileAccess.Read)) | |
{ | |
using (var archive = new Archive(new ArchiveEntrySettings())) | |
{ | |
archive.CreateEntry("test.txt", source1); | |
archive.Save(zipFile); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (FileStream zipFile = File.Open(dataDir + "CompressDirectory_out.zip", FileMode.Create)) | |
{ | |
using (Archive archive = new Archive()) | |
{ | |
DirectoryInfo corpus = new DirectoryInfo(dataDir + "zipfolder"); | |
archive.CreateEntries(corpus); | |
archive.Save(zipFile); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (FileStream zipFile = File.Open(dataDir + "CompressMultipleFiles_out.zip", FileMode.Create)) | |
{ | |
using (FileStream source1 = File.Open(dataDir + "test.txt", FileMode.Open, FileAccess.Read)) | |
{ | |
using (FileStream source2 = File.Open(dataDir + "test2.txt", FileMode.Open, FileAccess.Read)) | |
{ | |
using (var archive = new Archive()) | |
{ | |
archive.CreateEntry("test.txt", source1); | |
archive.CreateEntry("test1.txt", source2); | |
archive.Save(zipFile, new ArchiveSaveOptions() { Encoding = Encoding.ASCII, ArchiveComment = "There are two poems from Canterbury corpus" }); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment