/// <summary> /// Append the zip data to the file-entry specified. /// </summary> /// <param name="path"></param> /// <param name="entry"></param> /// <param name="data"></param> /// <returns></returns> public static bool ZipCreateAppendData(string path, string entry, string data) { try { if (File.Exists(path)) { using (var zip = ZipFile.Read(path)) { zip.AddEntry(entry, data); zip.Save(); } } else { using (var zip = new ZipFile(path)) { zip.AddEntry(entry, data); zip.Save(); } } } catch (Exception err) { Log.Error(err); return(false); } return(true); }
/// <summary> /// Return the entry file names contained in a zip file /// </summary> /// <param name="zipFileStream">Stream to the file</param> /// <returns>IEnumerable of entry file names</returns> public static IEnumerable <string> GetZipEntryFileNames(Stream zipFileStream) { using (var zip = ZipFile.Read(zipFileStream)) { return(zip.EntryFileNames); } }
} // End UnZip /// <summary> /// Unzip a stream that represents a zip file and return the first entry as a stream /// </summary> public static Stream UnzipStream(Stream zipstream, out ZipFile zipFile, string entryName = null) { zipFile = ZipFile.Read(zipstream); try { Ionic.Zip.ZipEntry entry; if (string.IsNullOrEmpty(entryName)) { //Read the file entry into buffer: entry = zipFile.Entries.FirstOrDefault(); } else { // Attempt to find our specific entry if (!zipFile.ContainsEntry(entryName)) { return(null); } entry = zipFile[entryName]; } if (entry != null) { return(entry.OpenReader()); } } catch (Exception err) { Log.Error(err); } return(null); } // End UnZip
/// <summary> /// Returns the entry file names contained in a zip file /// </summary> /// <param name="zipFileName">The zip file name</param> /// <returns>An IEnumerable of entry file names</returns> public static IEnumerable <string> GetZipEntryFileNames(string zipFileName) { using (var zip = ZipFile.Read(zipFileName)) { foreach (var entry in zip) { yield return(entry.FileName); } } }
/// <summary> /// Lazily unzips the specified stream /// </summary> /// <param name="stream">The zipped stream to be read</param> /// <returns>An enumerable whose elements are zip entry key value pairs with /// a key of the zip entry name and the value of the zip entry's file lines</returns> public static IEnumerable <KeyValuePair <string, IEnumerable <string> > > Unzip(Stream stream) { using (var zip = ZipFile.Read(stream)) { foreach (var entry in zip) { yield return(new KeyValuePair <string, IEnumerable <string> >(entry.FileName, ReadZipEntry(entry))); } } }
private static IEnumerable <KeyValuePair <string, IEnumerable <string> > > ReadLinesImpl(string filename, bool firstEntryOnly = false) { using (var zip = ZipFile.Read(filename)) { if (firstEntryOnly) { var entry = zip[0]; yield return(new KeyValuePair <string, IEnumerable <string> >(entry.FileName, ReadZipEntry(entry))); yield break; } foreach (var entry in zip) { yield return(new KeyValuePair <string, IEnumerable <string> >(entry.FileName, ReadZipEntry(entry))); } } }
} // End UnZip /// <summary> /// Unzip a stream that represents a zip file and return the first entry as a stream /// </summary> public static Stream UnzipStream(Stream zipstream, out ZipFile zipFile) { zipFile = ZipFile.Read(zipstream); try { //Read the file entry into buffer: var entry = zipFile.Entries.FirstOrDefault(); if (entry != null) { return(entry.OpenReader()); } } catch (Exception err) { Log.Error(err); } return(null); } // End UnZip
/// <summary> /// Append the zip data to the file-entry specified. /// </summary> /// <param name="path">The zip file path</param> /// <param name="entry">The entry name</param> /// <param name="data">The entry data</param> /// <param name="overrideEntry">True if should override entry if it already exists</param> /// <returns>True on success</returns> public static bool ZipCreateAppendData(string path, string entry, string data, bool overrideEntry = false) { try { using (var zip = File.Exists(path) ? ZipFile.Read(path) : new ZipFile(path)) { if (zip.ContainsEntry(entry) && overrideEntry) { zip.RemoveEntry(entry); } zip.AddEntry(entry, data); zip.Save(); } } catch (Exception err) { Log.Error(err); return(false); } return(true); }
private async Task <LottieResult <LottieComposition> > FetchFromNetworkInternalAsync(RenderTarget renderTarget, CancellationToken cancellationToken = default(CancellationToken)) { Debug.WriteLine($"Fetching {_url}", LottieLog.Tag); using (var connection = new HttpClient()) { using (var response = await connection.GetAsync(_url, cancellationToken)) { if (!response.IsSuccessStatusCode) { return(new LottieResult <LottieComposition>(new ArgumentException($"Unable to fetch {_url}. Failed with {response.StatusCode}\n{response.ReasonPhrase}"))); } //StorageFile file; FileExtension extension; LottieResult <LottieComposition> result; switch (response.Content.Headers.ContentType.MediaType) { case "application/zip": Debug.WriteLine("Handling zip response.", LottieLog.Tag); extension = FileExtension.Zip; //file = await _networkCache.WriteTempCacheFileAsync(await response.Content.ReadAsStreamAsync().AsAsyncOperation().AsTask(cancellationToken), extension, cancellationToken); //using (var stream = await file.OpenStreamForReadAsync().AsAsyncOperation().AsTask(cancellationToken)) //{ result = await LottieCompositionFactory.FromZipStreamAsync(renderTarget, ZipFile.Read(await response.Content.ReadAsStreamAsync()), _url); //} break; case "application/json": default: Debug.WriteLine("Received json response.", LottieLog.Tag); extension = FileExtension.Json; //file = await _networkCache.WriteTempCacheFileAsync(await response.Content.ReadAsStreamAsync().AsAsyncOperation().AsTask(cancellationToken), extension, cancellationToken); //using (var stream = await file.OpenStreamForReadAsync().AsAsyncOperation().AsTask(cancellationToken)) //{ result = await LottieCompositionFactory.FromJsonInputStreamAsync(await response.Content.ReadAsStreamAsync(), _url); //} break; } if (result.Value != null) { await _networkCache.RenameTempFileAsync(extension, cancellationToken); } Debug.WriteLine($"Completed fetch from network. Success: {result.Value != null}", LottieLog.Tag); return(result); } } }