public static PluginPackage FromArchive(ZipFile archive) { var package = (PluginPackage)null; var contentsEntry = archive.FirstOrDefault(file => file.FileName == "contents.xml"); if (contentsEntry == null) throw new FileNotFoundException("'contents.xml' is missing!", "contents.xml"); using (var stream = contentsEntry.ToStream()) { var contents = PluginPackageContentsXml.FromStream(stream); package = new PluginPackage(contents.Id, contents.Name, contents.Version, contents.Target); foreach (var file in contents.Files) { var entry = archive.FirstOrDefault(a => a.FileName == file.Name); if (entry == null) throw new FileNotFoundException("The package file '" + file.Name + "' is missing!", file.Name); package.files.Add(new PluginPackageFile(file.Name, entry.ToBytes())); } foreach (var reference in contents.References) { package.references.Add(new PluginPackageReference(reference.Id, reference.Version)); } } return package; }
public static byte[] ReadFileFromZip(ZipFile zip, string filename) { if (zip.ContainsEntry (filename)) { var entry = zip.FirstOrDefault (x => x.FileName == filename); if (entry != null) { using (var ms = new MemoryStream ()) { entry.Extract (ms); return ms.ToArray (); } } } return null; }
/// <summary> /// Streams a local zip file using a streamreader. /// Important: the caller must call Dispose() on the returned ZipFile instance. /// </summary> /// <param name="filename">Location of the original zip file</param> /// <param name="zipEntryName">The zip entry name to open a reader for. Specify null to access the first entry</param> /// <param name="zip">The ZipFile instance to be returned to the caller</param> /// <returns>Stream reader of the first file contents in the zip file</returns> public static StreamReader Unzip(string filename, string zipEntryName, out ZipFile zip) { StreamReader reader = null; zip = null; try { if (File.Exists(filename)) { try { zip = new ZipFile(filename); var entry = zip.FirstOrDefault(x => zipEntryName == null || string.Compare(x.FileName, zipEntryName, StringComparison.OrdinalIgnoreCase) == 0); if (entry == null) { // Unable to locate zip entry return(null); } reader = new StreamReader(entry.OpenReader()); } catch (Exception err) { Log.Error(err, "Inner try/catch"); if (zip != null) { zip.Dispose(); } if (reader != null) { reader.Close(); } } } else { Log.Error($"Data.UnZip(2): File doesn\'t exist: {filename}"); } } catch (Exception err) { Log.Error(err, "File: " + filename); } return(reader); }
public Reference( ITaskItem item ) { FullPath = item.GetMetadata( "FullPath" ); Zip = new ZipFile( FullPath ); var appdef = Zip.FirstOrDefault( ze => ze.FileName.ToLowerInvariant() == "appdef.xml" ); if( appdef == null ) throw new FileNotFoundException( string.Format( "The package definition file 'appdef.xml' was not found in the referenced package '{0}'", Path.GetFileName( FullPath ) ) ); var appdefStream = new MemoryStream(); appdef.Extract( appdefStream ); appdefStream.Position = 0; AppDef = XDocument.Load(appdefStream); PackageName = AppDef.Element( "application" ).Element( "name" ).Value; }
/// <summary> /// Streams a local zip file using a streamreader. /// Important: the caller must call Dispose() on the returned ZipFile instance. /// </summary> /// <param name="filename">Location of the original zip file</param> /// <param name="zipEntryName">The zip entry name to open a reader for. Specify null to access the first entry</param> /// <param name="zip">The ZipFile instance to be returned to the caller</param> /// <returns>Stream reader of the first file contents in the zip file</returns> public static StreamReader Unzip(string filename, string zipEntryName, out ZipFile zip) { StreamReader reader = null; zip = null; try { if (File.Exists(filename)) { try { zip = new ZipFile(filename); var entry = zip.FirstOrDefault(x => zipEntryName == null || string.Compare(x.FileName, zipEntryName, StringComparison.OrdinalIgnoreCase) == 0); if (entry == null) { Log.Error("Compression.Unzip(): Unable to locate zip entry with name: " + zipEntryName + " in file: " + filename); return null; } reader = new StreamReader(entry.OpenReader()); } catch (Exception err) { Log.Error(err, "Inner try/catch"); if (zip != null) zip.Dispose(); if (reader != null) reader.Close(); } } else { Log.Error("Data.UnZip(2): File doesn't exist: " + filename); } } catch (Exception err) { Log.Error(err, "File: " + filename); } return reader; }