Skip to content

Instantly share code, notes, and snippets.

@aspose-zip-gists
Last active August 19, 2024 10:11
Show Gist options
  • Save aspose-zip-gists/d69b478235af94b9860be5443f24d031 to your computer and use it in GitHub Desktop.
Save aspose-zip-gists/d69b478235af94b9860be5443f24d031 to your computer and use it in GitHub Desktop.
Archive related
using (var archive = new Archive())
{
archive.CreateEntries(@"D:\BigFolder");
EventsBag eb = new EventsBag();
eb.EntryCompressed += delegate(object sender, CancelEntryEventArgs args)
{
if (args.Entry.Name == @"BigFolder\last.bin"))
args.Cancel = true;
};
archive.Save("output.zip", new ArchiveSaveOptions() { EventsBag = eb });
}
using (var archive = new Archive())
{
Stopwatch sw = new Stopwatch();
archive.CreateEntries(@"D:\BigFolder");
EventsBag eb = new EventsBag();
eb.EntryCompressed += delegate(object sender, CancelEntryEventArgs args)
{
if (sw.Elapsed > TimeSpan.FromSeconds(60))
args.Cancel = true;
};
sw.Start();
archive.Save("output.zip", new ArchiveSaveOptions() { EventsBag = eb });
}
using (FileStream bz2File = File.Open("archive.bz2", FileMode.Create))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (Bzip2Archive archive = new Bzip2Archive())
{
archive.SetSource(source);
archive.Save(bz2File, new Bzip2SaveOptions(9) { CompressionThreads = 4 });
}
}
}
using (LzipArchive archive = new LzipArchive())
{
archive.SetSource("data.bin");
archive.Save("data.bin.lz");
}
using (FileStream lzFile = File.Open("archive.lz", FileMode.Create))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
var settings = new LzipArchiveSettings(16777216) { CompressionThreads = 4 }
using (LzipArchive archive = new LzipArchive(settings))
{
archive.SetSource(source);
archive.Save(lzFile);
}
}
}
FileInfo fi1 = new FileInfo("alice29.txt");
FileInfo fi2 = new FileInfo("picture.png");
using (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipPPMdCompressionSettings())))
{
archive.CreateEntry("alice29.txt", fi1);
archive.CreateEntry("picture.png", fi2);
archive.SaveSplit(@"C:\Folder", new SplitSevenZipArchiveSaveOptions("volume", 65536));
}
FileInfo fi1 = new FileInfo("alice29.txt");
FileInfo fi2 = new FileInfo("Bible.txt");
using (var archive = new Archive(new ArchiveEntrySettings(new DeflateCompressionSettings())))
{
archive.CreateEntry("alice29.txt", fi1);
archive.CreateEntry("Bible.txt", fi2);
archive.SaveSplit(@"C:\destination\", new SplitArchiveSaveOptions("part", 65536));
}
using (FileStream zipFile = File.Open("archive.exe", FileMode.Create))
{
using (var archive = new Archive())
{
archive.CreateEntry("entry.bin", "data.bin");
var sfxOptions = new SelfExtractorOptions()
{
ExtractorTitle = "Extractor",
CloseWindowOnExtraction = true,
TitleIcon = "C:\pictorgam.ico"
};
archive.Save(zipFile, new ArchiveSaveOptions() { SelfExtractorOptions = sfxOptions });
}
}
using (var archive = new SharArchive())
{
archive.CreateEntry("alice.txt", File.OpenRead("alice.txt"));
archive.CreateEntry("data.bin", File.OpenRead("block.raw"));
archive.Save("result.shar");
}
FileInfo fi1 = new FileInfo("alice29.txt");
FileInfo fi2 = new FileInfo("asyoulik.txt");
using (var archive = new XarArchive(new XarZlibCompressionSettings()))
{
archive.CreateEntry("alice29.txt", fi1, false);
archive.CreateEntry("fields.c", fi2, false);
archive.Save(xarFile);
}
using (FileStream xzFile = File.Open("data.bin.xz", FileMode.Create))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new XzArchive(Aspose.Zip.Xz.Settings.XzArchiveSettings.FastestSpeed))
{
archive.SetSource(source);
archive.Save(xzFile);
}
}
}
using (FileStream xzFile = File.Open("archive.xz", FileMode.Create))
{
using (FileStream source = File.Open("data.bin", FileMode.Open, FileAccess.Read))
{
var settings = new XzArchiveSettings() { CheckType = XzCheckType.Crc32, CompressionThreads = 4 };
using (var archive = new XzArchive(settings))
{
archive.SetSource(source);
archive.Save(xzFile);
}
}
}
using (FileStream source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
{
using (ZArchive archive = new ZArchive())
{
archive.SetSource(source);
archive.Save("alice29.txt.Z");
}
}
MemoryStream ms = new MemoryStream();
using (FileStream source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
{
using (ZArchive archive = new ZArchive())
{
archive.SetSource(source);
archive.Save(ms);
}
}
using (Archive zip = new Archive())
{
using (RarArchive rar = new RarArchive(@"D:\archvie.rar"))
{
for (int i = 0; i < rar.Entries.Count; i++)
{
if (!rar.Entries[i].IsDirectory)
{
var ms = new MemoryStream();
rar.Entries[i].Extract(ms);
ms.Seek(0, SeekOrigin.Begin);
zip.CreateEntry(rar.Entries[i].Name, ms);
}
else
zip.CreateEntry(rar.Entries[i].Name + "/", Stream.Null);
}
}
zip.Save("output.zip");
}
using (Archive source = new Archive("source.zip"))
{
using (var archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings())))
{
foreach (ArchiveEntry entry in source.Entries)
{
if (!entry.IsDirectory)
{
MemoryStream mem = new MemoryStream();
entry.Open().CopyTo(mem);
mem.Seek(0, SeekOrigin.Begin);
archive.CreateEntry(entry.Name, mem);
}
}
archive.Save("result.7z");
}
}
using (Archive source = new Archive("source.zip"))
{
using (TarArchive tar = new TarArchive())
{
foreach (ArchiveEntry entry in source.Entries)
{
if (!entry.IsDirectory)
{
MemoryStream mem = new MemoryStream();
entry.Open().CopyTo(mem);
tar.CreateEntry(entry.Name, mem);
}
}
tar.SaveGzipped("result.tar.gz");
}
}
using (var archive = new CpioArchive())
{
archive.CreateEntries(@"C:\folder", false);
archive.SaveXzCompressed("result.cpio.xz");
}
using (var archive = new CpioArchive())
{
archive.CreateEntries(@"C:\folder", false);
archive.SaveGzipped("result.cpio.gz");
}
using (FileStream cpioFile = File.Open("combined.cpio", FileMode.Create))
{
FileInfo fi1 = new FileInfo("alice29.txt");
FileInfo fi2 = new FileInfo("asyoulik.txt");
using (CpioArchive archive = new CpioArchive())
{
archive.CreateEntry("alice29.txt", fi1);
archive.CreateEntry("asyoulik.txt", fi2);
archive.Save(cpioFile, format);
}
}
public void OnPost(IFormFile uploadedFile, int rotateDegree)
{
ImageBytes = new List<byte[]>();
using (Archive archive = new Archive(uploadedFile.OpenReadStream()))
{
foreach (ArchiveEntry entry in archive.Entries)
{
if (entry.IsDirectory)
continue;
using (MemoryStream extracted = new MemoryStream())
{
entry.Open().CopyTo(extracted);
extracted.Seek(0, SeekOrigin.Begin);
using (MemoryStream rotated = new MemoryStream())
{
using (RasterImage image = (RasterImage)Image.Load(extracted))
{
if (!image.IsCached) // Before rotation, the image should be cached for better performance
image.CacheData();
image.Rotate(rotateDegree, true, Color.CornflowerBlue);
image.Save(rotated);
}
this.ImageBytes.Add(rotated.ToArray());
}
}
}
}
}
using (var archive = new CabArchive("installer.cab"))
{
archive.ExtractToDirectory(@"C:\extracted");
}
public void OnPost(IFormFile uploadedFile)
{
ImageBytes = new List<byte[]>();
using (Archive archive = new Archive(uploadedFile.OpenReadStream()))
{
foreach (var entry in archive.Entries.Where(e => e.Name.StartsWith(@"word/media", StringComparison.InvariantCultureIgnoreCase)))
{
using (MemoryStream extracted = new MemoryStream())
{
entry.Open().CopyTo(extracted);
extracted.Seek(0, SeekOrigin.Begin);
if (Aspose.Imaging.Image.CanLoad(extracted))
ImageBytes.Add(extracted.ToArray());
}
}
}
}
using (FileStream sourceLzipFile = File.Open("data.bin.lz", FileMode.Open))
{
using (FileStream extractedFile = File.Open("data.bin", FileMode.Create))
{
using (LzipArchive archive = new LzipArchive(sourceLzipFile))
{
archive.Extract(extractedFile);
}
}
}
using (SevenZipArchive archive = new SevenZipArchive(new string[] { "part.7z.001", "part.7z.002", "part.7z.003" }))
{
archive.ExtractToDirectory(@"C:\extracted");
}
using (Archive a = new Archive("part.zip", new string[] { "part.z01", "part.z02" }))
{
a.ExtractToDirectory(@"C:\extracted\")
}
using(WimArchive wimArchive = new WimArchive("boot.wim"))
{
foreach (WimFileEntry entry in wimArchive.Images[0].AllEntries.OfType<WimFileEntry>())
{
string entryPath = Path.Combine("extracted", entry.Name);
entry.Extract(entryPath);
}
}
using (WimArchive wimArchive = new WimArchive("boot.wim"))
{
wimArchive.Images[0].ExtractToDirectory("extracted0");
wimArchive.Images[1].ExtractToDirectory("extracted1");
}
if (!Directory.Exists("C:\\extracted"))
{
Directory.CreateDirectory("C:\\extracted");
}
using(XarArchive xarArchive = new XarArchive("data.xar"))
{
foreach (XarEntry entry in xarArchive.Entries)
{
if (entry is XarFileEntry xarFileEntry)
{
var entryPath = Path.Combine("C:\\extracted", entry.FullPath);
xarFileEntry.Extract(entryPath);
}
else if (entry is XarDirectoryEntry)
{
var entryPath = Path.Combine("C:\\extracted", entry.FullPath);
Directory.CreateDirectory(entryPath);
}
}
}
using (XarArchive xarArchive = new XarArchive("data.xar"))
{
xarArchive.ExtractToDirectory("data");
}
using (var archive = new XzArchive("data.bin.xz"))
{
archive.Extract("data.bin");
}
FileInfo fi = new FileInfo("data.bin.Z");
using (ZArchive archive = new ZArchive(fi.OpenRead()))
{
archive.Extract("data.bin");
}
using (Archive outer = new Archive("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)
{
if (entry.Name.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase)) // Find an entry which is an archive itself
{
entriesToDelete.Add(entry); // Keep reference to the entry in order to remove it from the archive later
MemoryStream innerCompressed = new MemoryStream();
entry.Open().CopyTo(innerCompressed); //This extracts the entry to a memory stream
using (Archive inner = new Archive(innerCompressed)) // We know that content of the entry is an zip archive so we may extract
{
foreach (ArchiveEntry ie in inner.Entries) // Loop over entries of inner archive
{
namesToInsert.Add(ie.Name); // Keep the name of inner entry.
MemoryStream content = new MemoryStream();
ie.Open().CopyTo(content);
contentToInsert.Add(content); // Keep the content of inner entry.
}
}
}
}
foreach (ArchiveEntry e in entriesToDelete)
outer.DeleteEntry(e); // Delete all the entries which are archives itself
for (int i = 0; i < namesToInsert.Count; i++)
outer.CreateEntry(namesToInsert[i], contentToInsert[i]); // Adds entries which were entries of inner archives
outer.Save("flatten.zip");
}
private Stream GenerateBarcode()
{
var generator = new Aspose.BarCode.Generation.BarcodeGenerator(
Aspose.BarCode.Generation.EncodeTypes.Pdf417, "This is a test code text. \n Second line \n third line.");
generator.Parameters.Barcode.XDimension.Millimeters = 0.6f;
generator.Parameters.Barcode.BarHeight.Millimeters = 1.2f;
MemoryStream result = new MemoryStream();
generator.Save(result, Aspose.BarCode.Generation.BarCodeImageFormat.Bmp);
result.Seek(0, SeekOrigin.Begin);
return result;
}
string template = "T0p$ecret{0}{1}";
for (char c = 'A'; c < 'Z'; c++)
{
bool correct = false;
for (int i = 10; i < 99; i++)
{
string password = string.Format(template, c, i);
try
{
using (Archive a = new Archive("encrypted.zip", new ArchiveLoadOptions() {DecryptionPassword = password}))
a.ExtractToDirectory(".");
correct = true;
}
catch (System.IO.InvalidDataException e)
{
correct = false;
}
if (correct)
{
Console.WriteLine($"Proper password: {password}");
break;
}
}
if (correct)
break;
}
public FileStreamResult OnPost([FromForm] ArchiveFormat archiveFormat)
{
using (var barcode = this.GenerateBarcode())
{
var result = new MemoryStream();
switch (archiveFormat)
{
case ArchiveFormat.Zip:
using (Archive a = new Archive())
{
a.CreateEntry("barcode.bmp", barcode);
a.Save(result);
}
result.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(result, "application/zip") {FileDownloadName = "barcode.zip"};
case ArchiveFormat.Bzip2:
using (Bzip2Archive a = new Bzip2Archive())
{
a.SetSource(barcode);
a.Save(result);
}
result.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(result, "application/x-bzip2") {FileDownloadName = "barcode.bmp.bz2"};
case ArchiveFormat.SevenZip:
using (SevenZipArchive a = new SevenZipArchive())
{
a.CreateEntry("barcode.bmp", barcode);
a.Save(result);
}
result.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(result, "application/x-7z-compressed") {FileDownloadName = "barcode.7z"};
default:
throw new ArgumentOutOfRangeException(nameof(archiveFormat));
}
}
}
string[] archivesPaths = new string[] { "data/first.tar.gz",
"data/second.tar.gz", "data/third.tar.gz" };
TarArchive[] archives = new TarArchive[archivesPaths.Length];
using (TarArchive mergred = new TarArchive())
{
for (int i = 0; i < archivesPaths.Length; i++)
{
TarArchive a = TarArchive.FromGZip(archivesPaths[i]);
archives[i] = a;
foreach (TarEntry entry in a.Entries)
mergred.CreateEntry(entry.Name, entry.Open());
}
mergred.SaveGzipped("merged.tar.gz");
for (int i = 0; i < archivesPaths.Length; i++)
{
archives[i].Dispose();
}
}
string[] archivesPaths = new string[] { "data/first.zip", "data/second.zip" };
Archive[] archives = new Archive[archivesPaths.Length];
using (var merged = new Archive())
{
for (int i = 0; i < archivesPaths.Length; i++)
{
Archive a = new Archive(archivesPaths[i]);
archives[i] = a;
foreach (ArchiveEntry entry in a.Entries)
merged.CreateEntry(entry.Name, entry.Open());
}
merged.Save("merged.zip");
for (int i = 0; i < archivesPaths.Length; i++)
{
archives[i].Dispose();
}
}
using (Bzip2Archive archive = new Bzip2Archive())
{
archive.SetSource("data.bin");
archive.Save("result.bz2", new Bzip2SaveOptions() { CompressionThreads = Environment.ProcessorCount });
}
using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
using (Archive archive = new Archive())
{
archive.CreateEntry("first.bin", File.OpenRead("data1.bin"));
...
archive.CreateEntry("last.bin", File.OpenRead("dataN.bin"));
archive.Save(zipFile, new ArchiveSaveOptions()
{
ParallelOptions = new ParallelOptions()
{ ParallelCompressInMemory = ParallelCompressionMode.Always }
});
}
}
using (FileStream source = File.Open("huge.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings())))
{
SevenZipArchiveEntry entry = archive.CreateEntry("huge.bin", source);
int percentReady = 0;
entry.CompressionProgressed += delegate (object s, ProgressEventArgs e)
{
// s is SevenZipArchiveEntry
int percent = (int)((100 * (long)e.ProceededBytes) / source.Length);
if (percent > percentReady)
{
percentReady = percent;
}
};
archive.Save(zipFile);
}
}
using (FileStream source = File.Open("huge.bin", FileMode.Open, FileAccess.Read))
{
using (var archive = new Archive(new ArchiveEntrySettings(new PPMdCompressionSettings())))
{
ArchiveEntry entry = archive.CreateEntry("huge.bin", source);
int percentReady = 0;
entry.CompressionProgressed += (s, e) =>
{
int percent = (int)((100 * (long)e.ProceededBytes) / source.Length);
if (percent > percentReady)
{
Console.WriteLine(string.Format("{0}% compressed", percent));
percentReady = percent;
}
};
archive.Save(zipFile);
}
}
using (MemoryStream rotated = new MemoryStream())
{
using (RasterImage image = (RasterImage)Image.Load(extracted))
{
// Before rotation, the image should be cached for better performance
if (!image.IsCached)
image.CacheData();
image.Rotate(rotateDegree, true, Color.CornflowerBlue);
image.Save(rotated);
}
}
SevenZipEntrySettings settings = new SevenZipEntrySettings(new SevenZipLZMA2CompressionSettings(){ CompressionThreads = 4 });
using (var archive = new SevenZipArchive(settings))
{
archive.CreateEntry("first.bin", "data.bin");
archive.Save("result.7z");
}
using (FileStream zipFile = File.Open("archive.7z", FileMode.Create))
{
FileInfo fi1 = new FileInfo("alice29.txt");
FileInfo fi2 = new FileInfo("asyoulik.txt");
using (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipStoreCompressionSettings())))
{
archive.CreateEntry("alice29.txt", fi1);
archive.CreateEntry("asyoulik.txt", fi2);
archive.Save(zipFile);
}
}
using (FileStream xarFile = File.Open("archive.xar", FileMode.Create))
{
FileInfo fi1 = new FileInfo("alice29.txt");
FileInfo fi2 = new FileInfo("asyoulik.txt");
using (var archive = new XarArchive(new XarStoreCompressionSettings()))
{
archive.CreateEntry("alice29.txt", fi1);
archive.CreateEntry("asyoulik.txt", fi2);
archive.Save(xarFile);
}
}
using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
FileInfo fi1 = new FileInfo("alice29.txt");
FileInfo fi2 = new FileInfo("asyoulik.txt");
using (Archive archive = new Archive(new ArchiveEntrySettings(new StoreCompressionSettings())))
{
archive.CreateEntry("alice29.txt", fi1);
archive.CreateEntry("lcet10.txt", fi2);
archive.Save(zipFile);
}
}
using (FileStream xzFile = File.Open("archive.tar.xz", FileMode.Create))
{
using (var archive = new TarArchive())
{
archive.CreateEntry("text.txt", @"D:\texts\article.txt");
archive.CreateEntry("picture.png", @"D:\Picture\photo.png");
archive.SaveXzCompressed(xzFile);
}
}
using (var archive = new TarArchive())
{
archive.CreateEntry("text.txt", @"D:\texts\article.txt");
archive.CreateEntry("picture.png", @"D:\Picture\photo.png");
archive.SaveGzipped("result.tar.gz");
}
using (FileStream tarFile = File.Open("joint.tar", FileMode.Create))
{
FileInfo fi1 = new FileInfo("text.txt");
FileInfo fi2 = new FileInfo("picture.png"));
using (var archive = new TarArchive())
{
archive.CreateEntry("text.txt", fi1);
archive.CreateEntry("picture.png", fi2);
archive.Save(tarFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment