Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aspose.ZIP for .NET
This Gist contains examples for Aspose.ZIP for .NET in C# language.
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "CompressFilesByFileInfo_out.zip", FileMode.Create))
{
FileInfo fi1 = new FileInfo(dataDir + "alice29.txt");
FileInfo fi2 = new FileInfo(dataDir + "fields.c");
using (var archive = new Archive())
{
archive.CreateEntry("alice29.txt", fi1);
archive.CreateEntry("fields.c", fi2);
archive.Save(zipFile, new ArchiveSaveOptions() { Encoding = Encoding.ASCII });
}
}
using (FileStream zipFile = File.Open(dataDir + "Bzip2Compression_out.zip", FileMode.Create))
{
using (Archive archive = new Archive(new ArchiveEntrySettings(new Bzip2CompressionSettings())))
{
archive.CreateEntry("sample.txt", dataDir + "sample.txt");
archive.Save(zipFile);
}
}
using (FileStream zipFile = File.Open(dataDir + "LZMACompression_out.zip", FileMode.Create))
{
using (Archive archive = new Archive(new ArchiveEntrySettings(new LzmaCompressionSettings())))
{
archive.CreateEntry("sample.txt", dataDir + "sample.txt");
archive.Save(zipFile);
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "CompressMultipleFiles_out.zip", FileMode.Create))
{
using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
{
using (FileStream source2 = File.Open(dataDir + "asyoulik.txt", FileMode.Open, FileAccess.Read))
{
using (var archive = new Archive())
{
archive.CreateEntry("alice29.txt", source1);
archive.CreateEntry("asyoulik.txt", source2);
archive.Save(zipFile, new ArchiveSaveOptions() { Encoding = Encoding.ASCII, ArchiveComment = "There are two poems from Canterbury corpus" });
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "CompressSingleFile_out.zip", FileMode.Create))
{
//File to be added to archive
using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
{
using (var archive = new Archive(new ArchiveEntrySettings()))
{
archive.CreateEntry("alice29.txt", source1);
archive.Save(zipFile);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "CompressMultipleFiles_out.zip", FileMode.Open))
{
StringBuilder sb = new StringBuilder("Entries are: ");
int percentReady = 0;
using (Archive archive = new Archive(zipFile,
new ArchiveLoadOptions()
{
EntryListed = (s, e) => { sb.AppendFormat("{0}, ", e.Entry.Name); },
EntryExtractionProgressed = (s, e) =>
{
int percent = (int)((100 * e.ProceededBytes) / ((ArchiveEntry)s).UncompressedSize);
if (percent > percentReady)
{
Console.WriteLine(string.Format("{0}% compressed", percent)); percentReady = percent;
}
}
}))
{
Console.WriteLine(sb.ToString(0, sb.Length - 2));
using (var extracted = File.Create(dataDir + "alice_extracted_out.txt"))
{
using (var decompressed = archive.Entries[0].Open())
{
byte[] buffer = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.Read(buffer, 0, buffer.Length)))
{
extracted.Write(buffer, 0, bytesRead);
}
// Read from decompressed stream to extracting file.
}
}
percentReady = 0;
archive.Entries[1].Extract(dataDir + "asyoulik_extracted_out.txt");
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream fs = File.OpenRead(dataDir + "CompressSingleFile_out.zip"))
{
using (Archive archive = new Archive(fs))
{
int percentReady = 0;
archive.Entries[0].ExtractionProgressed += (s, e) =>
{
int percent = (int)((100 * e.ProceededBytes) / ((ArchiveEntry)s).UncompressedSize);
if (percent > percentReady)
{
Console.WriteLine(string.Format("{0}% decompressed", percent));
percentReady = percent;
}
};
archive.Entries[0].Extract(dataDir + "alice_extracted_out.txt");
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "StoreMultipleFilesWithoutCompression_out.zip", FileMode.Open))
{
using (Archive archive = new Archive(zipFile))
{
using (var extracted = File.Create(dataDir + "alice_extracted_store_out.txt"))
{
using (var decompressed = archive.Entries[0].Open())
{
byte[] buffer = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.Read(buffer, 0, buffer.Length)))
{
extracted.Write(buffer, 0, bytesRead);
}
// Read from decompressed stream to extracting file.
}
}
using (var extracted = File.Create(dataDir + "asyoulik_extracted_store_out.txt"))
{
using (var decompressed = archive.Entries[1].Open())
{
byte[] buffer = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.Read(buffer, 0, buffer.Length)))
{
extracted.Write(buffer, 0, bytesRead);
}
// Read from decompressed stream to extracting file.
}
}
}
}
string dataDir = RunExamples.GetDataDir_Data();
using (Archive outer = new Archive(dataDir + "outer.zip"))
{
List<ArchiveEntry> entriesToDelete = new List<ArchiveEntry>();
List<string> namesToInsert = new List<string>();
List<MemoryStream> contentToInsert = new List<MemoryStream>();
foreach (ArchiveEntry entry in outer.Entries)
{
// Find an entry which is an archive itself
if (entry.Name.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase) /* or another condition */ )
{
// Keep reference to the entry in order to remove it from the archive later
entriesToDelete.Add(entry);
MemoryStream innerCompressed = new MemoryStream();
//This extracts the entry to a memory stream
entry.Open().CopyTo(innerCompressed);
// We know that content of the entry is an zip archive so we may extract
using (Archive inner = new Archive(innerCompressed))
{
// Loop over entries of inner archive
foreach (ArchiveEntry ie in inner.Entries)
{
// Keep the name of inner entry.
namesToInsert.Add(ie.Name);
MemoryStream content = new MemoryStream();
ie.Open().CopyTo(content);
// Keep the content of inner entry.
contentToInsert.Add(content);
}
}
}
}
// Delete all the entries which are archives itself
foreach (ArchiveEntry e in entriesToDelete)
{
outer.DeleteEntry(e);
}
for (int i = 0; i < namesToInsert.Count; i++)
{
// Adds entries which were entries of inner archives
outer.CreateEntry(namesToInsert[i], contentToInsert[i]);
}
outer.Save(dataDir + "flatten.zip");
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
//Creates zip archive without compressing files
using (FileStream zipFile = File.Open(dataDir + "StoreMultipleFilesWithoutCompression_out.zip", FileMode.Create))
{
FileInfo fi1 = new FileInfo(dataDir + "alice29.txt");
FileInfo fi2 = new FileInfo(dataDir + "lcet10.txt");
using (Archive archive = new Archive(new ArchiveEntrySettings(new StoreCompressionSettings())))
{
archive.CreateEntry("alice29.txt", fi1);
archive.CreateEntry("lcet10.txt", fi2);
archive.Save(zipFile, new ArchiveSaveOptions() { Encoding = Encoding.ASCII });
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "UsingParallelismToCompressFiles_out.zip", FileMode.Create))
{
using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
{
using (FileStream source2 = File.Open(dataDir + "asyoulik.txt", FileMode.Open, FileAccess.Read))
{
using (var archive = new Archive())
{
archive.CreateEntry("alice29.txt", source1);
archive.CreateEntry("asyoulik.txt", source2);
//Define the parallelism criterion
var parallelOptions = new ParallelOptions
{
ParallelCompressInMemory = ParallelCompressionMode.Always
};
archive.Save(zipFile,
new ArchiveSaveOptions()
{
ParallelOptions = parallelOptions,
Encoding = Encoding.ASCII,
ArchiveComment = "There are two poems from Canterbury corpus"
});
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (FileStream zipFile = File.Open(dataDir + "CompressDirectory_out.zip", FileMode.Create))
{
using (FileStream zipFile2 = File.Open(dataDir + "CompressDirectory2_out.zip", FileMode.Create))
{
using (Archive archive = new Archive())
{
DirectoryInfo corpus = new DirectoryInfo(dataDir + "CanterburyCorpus");
archive.CreateEntries(corpus);
archive.Save(zipFile);
archive.Save(zipFile2);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "CompressDirectory_out.zip", FileMode.Open))
{
using (var archive = new Archive(zipFile))
{
archive.ExtractToDirectory(dataDir + "DecompressFolder_out");
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "\\different_password.zip", FileMode.Open))
{
using (Archive archive = new Archive(zipFile))
{
archive.Entries[0].Extract(dataDir + "alice29_extracted_pass_out.txt", "first_pass");
archive.Entries[1].Extract(dataDir + "asyoulik_extracted_pass_out.txt", "second_pass");
}
}
using (GzipArchive archive = new GzipArchive())
{
archive.SetSource(dataDir + "data.bin");
archive.Save(dataDir + "archive.gz");
}
//Open an archive from a stream and extract it to a MemoryStream
var ms = new MemoryStream();
using (GzipArchive archive = new GzipArchive(File.OpenRead(dataDir + "sample.gz")))
{
archive.Open().CopyTo(ms);
Console.WriteLine(archive.Name);
}
//Extracts the archive and copies extracted content to file stream.
using (var archive = new GzipArchive(dataDir + "archive.gz"))
{
using (var extracted = File.Create(dataDir + "data.bin"))
{
var unpacked = archive.Open();
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = unpacked.Read(b, 0, b.Length)))
extracted.Write(b, 0, bytesRead);
}
}
//Writes compressed data to http response stream.
var ms = new MemoryStream();
using (var archive = new GzipArchive())
{
archive.SetSource(new FileInfo(dataDir + "data.bin"));
archive.Save(ms);
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "CompressFilesWithIndividualPasswords_out.zip", FileMode.Create))
{
FileInfo source1 = new FileInfo(dataDir + "alice29.txt");
FileInfo source2 = new FileInfo(dataDir + "asyoulik.txt");
FileInfo source3 = new FileInfo(dataDir + "fields.c");
using (var archive = new Archive())
{
archive.CreateEntry("alice29.txt", source1, true, new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")));
archive.CreateEntry("asyoulik.txt", source2, true, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEcryptionSettings("pass2", EncryptionMethod.AES128)));
archive.CreateEntry("fields.c", source3, true, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEcryptionSettings("pass3", EncryptionMethod.AES256)));
archive.Save(zipFile);
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(".\\CompressMultipleFilesWithTraditionalEncryption_out.zip", FileMode.Create))
{
FileInfo source1 = new FileInfo(".\\CanterburyCorpus\\alice29.txt");
FileInfo source2 = new FileInfo(".\\CanterburyCorpus\\asyoulik.txt");
FileInfo source3 = new FileInfo(".\\CanterburyCorpus\\fields.c");
using (var archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("p@s$"))))
{
archive.CreateEntry("alice29.txt", source1);
archive.CreateEntry("asyoulik.txt", source2);
archive.CreateEntry("fields.c", source3);
archive.Save(zipFile);
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream fs = File.OpenRead(dataDir + "PasswordProtectWithAES256_out.zip"))
{
using (var extracted = File.Create(dataDir + "alice_aesextracted_out.txt"))
{
using (Archive archive = new Archive(fs))
{
using (var decompressed = archive.Entries[0].Open("p@s$"))
{
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
{
extracted.Write(b, 0, bytesRead);
}
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream fs = File.OpenRead(dataDir + "StoreMutlipleFilesWithoutCompressionWithPassword_out.zip"))
{
using (var extracted = File.Create(dataDir + "alice_aesextracted_out.txt"))
{
using (Archive archive = new Archive(fs, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
{
using (var decompressed = archive.Entries[0].Open())
{
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
{
extracted.Write(b, 0, bytesRead);
}
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(".\\all_corpus_encrypted.zip", FileMode.Open))
{
new Archive(zipFile, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }).ExtractToDirectory(".\\all_corpus_decrypted");
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream fs = File.OpenRead(dataDir + "CompressWithTraditionalEncryption_out.zip"))
{
using (var extracted = File.Create(dataDir + "alice_extracted_out.txt"))
{
using (Archive archive = new Archive(fs, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
{
using (var decompressed = archive.Entries[0].Open())
{
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
{
extracted.Write(b, 0, bytesRead);
}
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(".\\all_corpus_encrypted_out.zip", FileMode.Create))
{
DirectoryInfo corpus = new DirectoryInfo(".\\CanterburyCorpus");
using (var archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("p@s$"))))
{
archive.CreateEntries(corpus);
archive.Save(zipFile);
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "PasswordProtectWithAES128_out.zip", FileMode.Create))
{
using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
{
using (var archive = new Archive(new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES128))))
{
archive.CreateEntry("alice29.txt", source1);
archive.Save(zipFile);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "PasswordProtectWithAES192_out.zip", FileMode.Create))
{
using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
{
using (var archive = new Archive(new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES192))))
{
archive.CreateEntry("alice29.txt", source1);
archive.Save(zipFile);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "PasswordProtectWithAES256_out.zip", FileMode.Create))
{
using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
{
using (var archive = new Archive(new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES256))))
{
archive.CreateEntry("alice29.txt", source1);
archive.Save(zipFile);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "CompressWithTraditionalEncryption_out.zip", FileMode.Create))
{
using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
{
var archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("p@s$")));
archive.CreateEntry("alice29.txt", source1);
archive.Save(zipFile);
}
}
// For complete examples and data files, please go to https://github.com/aspose-zip/Aspose.ZIP-for-.NET
using (FileStream zipFile = File.Open(dataDir + "StoreMutlipleFilesWithoutCompressionWithPassword_out.zip", FileMode.Create))
{
using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
{
using (var archive = new Archive(new ArchiveEntrySettings(new StoreCompressionSettings(), new AesEcryptionSettings("p@s$", EncryptionMethod.AES256))))
{
archive.CreateEntry("alice29.txt", source1);
archive.Save(zipFile);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.