/// <summary> /// Creates a <see cref="BlobTree" /> from the ZIP archive read from the specified <see cref="Stream" />. /// </summary> /// <param name="stream">The <see cref="Stream" /> from which to read the ZIP archive from.</param> /// <param name="leaveOpen">A <see cref="bool" /> value indicating whether to leave <paramref name="stream" /> open.</param> /// <returns> /// The <see cref="BlobTree" /> this method creates. /// </returns> public static BlobTree Decompress(Stream stream, bool leaveOpen) { Check.ArgumentNull(stream, nameof(stream)); BlobTree tree = new BlobTree(); using ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen); foreach (ZipArchiveEntry entry in archive.Entries) { BlobTreeNode node = tree.Root; foreach (string path in entry.FullName.TrimEndString(entry.Name, true, true).TrimEnd('\\').Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)) { if (!node.Nodes.HasNode(path, true)) { node.Nodes.Add(new BlobTreeNode(path)); } node = node.Nodes[path, true]; } node.Blobs.Add(new Blob(entry.Name, entry.GetContent())); } return(tree); }
/// <summary> /// Creates a ZIP archive from the specified <see cref="BlobTree" /> and returns a <see cref="byte" />[] representing the compressed archive. /// </summary> /// <param name="blobs">A tree of <see cref="Blob" /> objects to create the ZIP archive from.</param> /// <returns> /// A <see cref="byte" />[] representing the compressed ZIP archive with all <see cref="Blob" /> objects from the specified tree. /// </returns> public static byte[] Compress(BlobTree blobs) { Check.ArgumentNull(blobs, nameof(blobs)); using MemoryStream memoryStream = new MemoryStream(); Compress(blobs, memoryStream); return(memoryStream.ToArray()); }
public void BytecodeApi_IO_ZipCompression() { byte[] data1 = MathEx.RandomNumberGenerator.GetBytes(10000); byte[] data2 = MathEx.RandomNumberGenerator.GetBytes(10000); BlobTree tree = new BlobTree(); tree.Root.Blobs.Add(new Blob("file1.txt", data1)); tree.Root.Blobs.Add(new Blob("file2.txt", data2)); BlobTree decompressed = ZipCompression.Decompress(ZipCompression.Compress(tree)); Assert.IsTrue(tree.Root.Blobs[0].Compare(decompressed.Root.Blobs[0])); Assert.IsTrue(tree.Root.Blobs[1].Compare(decompressed.Root.Blobs[1])); }
/// <summary> /// Creates a ZIP archive from the specified <see cref="BlobTree" /> and writes the compressed archive to <paramref name="stream" />. /// </summary> /// <param name="blobs">A tree of <see cref="Blob" /> objects to create the ZIP archive from.</param> /// <param name="stream">The <see cref="Stream" /> to write the compressed archive to.</param> /// <param name="leaveOpen">A <see cref="bool" /> value indicating whether to leave <paramref name="stream" /> open.</param> public static void Compress(BlobTree blobs, Stream stream, bool leaveOpen) { Check.ArgumentNull(blobs, nameof(blobs)); Check.ArgumentNull(stream, nameof(stream)); using ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen); CreateEntries("", blobs.Root); void CreateEntries(string path, BlobTreeNode node) { foreach (BlobTreeNode childNode in node.Nodes) { CreateEntries(Path.Combine(path, childNode.Name), childNode); } foreach (Blob blob in node.Blobs) { archive.CreateEntry(Path.Combine(path, blob.Name), blob.Content); } } }
public static CsvData ImportFromCloud() { CsvData csvData = new CsvData(); BlobTree countryBlob = ZipCompression.Decompress(HttpClient.Default.CreateGetRequest("https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip").ReadBytes()); BlobTree asnBlob = ZipCompression.Decompress(HttpClient.Default.CreateGetRequest("https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN-CSV.zip").ReadBytes()); BlobTree cityBlob = ZipCompression.Decompress(HttpClient.Default.CreateGetRequest("https://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip").ReadBytes()); csvData.Country = FindBlob(countryBlob, "GeoLite2-Country-Locations-en.csv"); csvData.Range = FindBlob(countryBlob, "GeoLite2-Country-Blocks-IPv4.csv"); csvData.Range6 = FindBlob(countryBlob, "GeoLite2-Country-Blocks-IPv6.csv"); csvData.Asn = FindBlob(asnBlob, "GeoLite2-ASN-Blocks-IPv4.csv"); csvData.Asn6 = FindBlob(asnBlob, "GeoLite2-ASN-Blocks-IPv6.csv"); csvData.City = FindBlob(cityBlob, "GeoLite2-City-Locations-en.csv"); csvData.CityRange = FindBlob(cityBlob, "GeoLite2-City-Blocks-IPv4.csv"); csvData.CityRange6 = FindBlob(cityBlob, "GeoLite2-City-Blocks-IPv6.csv"); return(csvData); byte[] FindBlob(BlobTree blobs, string name) { return(blobs.Root.Nodes.First().Blobs[name].Content); } }
/// <summary> /// Creates a ZIP archive from the specified <see cref="BlobTree" /> and writes the compressed archive to <paramref name="stream" />. /// </summary> /// <param name="blobs">A tree of <see cref="Blob" /> objects to create the ZIP archive from.</param> /// <param name="stream">The <see cref="Stream" /> to write the compressed archive to.</param> public static void Compress(BlobTree blobs, Stream stream) { Compress(blobs, stream, false); }