本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipFile.Cast方法的典型用法代码示例。如果您正苦于以下问题:C# ZipFile.Cast方法的具体用法?C# ZipFile.Cast怎么用?C# ZipFile.Cast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.Zip.ZipFile
的用法示例。
在下文中一共展示了ZipFile.Cast方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OpenZipFile
public void OpenZipFile()
{
ZipFile zipFile = new ZipFile(zipFileName);
Dictionary<string, TestFile> xmlFiles = new Dictionary<string, TestFile>();
// Decompress XML files
foreach(ZipEntry zipEntry in zipFile.Cast<ZipEntry>().Where(zip => zip.IsFile && zip.Name.EndsWith(".xml"))) {
Stream stream = zipFile.GetInputStream(zipEntry);
string content = new StreamReader(stream).ReadToEnd();
xmlFiles.Add(zipEntry.Name, new TestFile { Name = zipEntry.Name, Content = content });
}
// Add descriptions
foreach(TestFile metaData in xmlFiles.Values.Where(f => f.Name.StartsWith("ibm/ibm_oasis"))) {
var doc = System.Xml.Linq.XDocument.Parse(metaData.Content);
foreach(var testElem in doc.Descendants("TEST")) {
string uri = "ibm/" + testElem.Attribute("URI").Value;
string description = testElem.Value.Replace("\n ", "\n").TrimStart('\n');
if (xmlFiles.ContainsKey(uri))
xmlFiles[uri].Description = description;
}
}
// Copy canonical forms
foreach(TestFile canonical in xmlFiles.Values.Where(f => f.Name.Contains("/out/"))) {
string uri = canonical.Name.Replace("/out/", "/");
if (xmlFiles.ContainsKey(uri))
xmlFiles[uri].Canonical = canonical.Content;
}
// Copy resuts to field
this.xmlFiles.AddRange(xmlFiles.Values.Where(f => !f.Name.Contains("/out/")));
}
示例2: GetPackageCreatedDateTime
public static DateTimeOffset GetPackageCreatedDateTime(Stream stream)
{
var zip = new ZipFile(stream);
return zip.Cast<ZipEntry>()
.Where(f => f.Name.EndsWith(".nuspec"))
.Select(f => f.DateTime)
.FirstOrDefault();
}
示例3: FindTheDocumentXmlEntry
private static ZipEntry FindTheDocumentXmlEntry(ZipFile zip, string documentXmlPath)
{
var entry = zip.Cast<ZipEntry>().FirstOrDefault(x => x.Name == documentXmlPath);
if (entry != null)
{
return entry;
}
throw new ArgumentException("unable to find " + documentXmlPath + " in file " + zip.Name);
}
示例4: LoadDescriptor
void LoadDescriptor()
{
using (var zip = new ZipFile(_wrapFile.FullName))
{
var descriptor = zip.Cast<ZipEntry>().FirstOrDefault(x => x.Name.EndsWith(".wrapdesc"));
if (descriptor == null)
throw new InvalidOperationException("The package '{0}' doesn't contain a valid .wrapdesc file.");
using (var stream = zip.GetInputStream(descriptor))
Descriptor = new WrapDescriptorParser().ParseFile(descriptor.Name, stream);
}
}
示例5: GetNupkgContentAsync
public static IObservable<Tuple<string, Func<Stream>>> GetNupkgContentAsync(Package package)
{
var o = CreateDownloadObservable(new Uri($"http://nuget.org/api/v2/package/{package.Name}/{package.Version}"));
return o.SelectMany(input => {
return Observable.Create<Tuple<ZipFile, ZipEntry>>(observer => {
var z = new ZipFile(new MemoryStream(input)) { IsStreamOwner = true };
var sub = Observable.ToObservable(z.Cast<ZipEntry>()).Select(ze => Tuple.Create(z, ze)).Subscribe(observer);
return new CompositeDisposable() { z, sub };
});
})
.Select(t => Tuple.Create<string, Func<Stream>>(t.Item2.Name.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar), () => t.Item1.GetInputStream(t.Item2)));
}
示例6: GetInternalCkan
public JObject GetInternalCkan(string filePath)
{
using (var zipfile = new ZipFile(filePath))
{
// Skip everything but embedded .ckan files.
var entries = zipfile
.Cast<ZipEntry>()
.Where(entry => Regex.IsMatch(entry.Name, ".CKAN$", RegexOptions.IgnoreCase));
foreach (var entry in entries)
{
Log.DebugFormat("Reading {0}", entry.Name);
using (var zipStream = zipfile.GetInputStream(entry))
{
return DeserializeFromStream(zipStream);
}
}
}
return null;
}
示例7: Content
public static IEnumerable<PackageContent> Content(Stream nuPackPackage)
{
PackageContent content = null;
string temporaryFile = null;
try
{
if (!nuPackPackage.CanSeek)
{
temporaryFile = Path.GetTempFileName();
using(var temporaryFileStream = File.OpenWrite(temporaryFile))
nuPackPackage.CopyTo(temporaryFileStream);
nuPackPackage = File.OpenRead(temporaryFile);
}
using (var inputZip = new ZipFile(nuPackPackage))
{
foreach (var entry in inputZip.Cast<ZipEntry>().Where(x => x.IsFile))
{
var segments = entry.Name.Split('/');
if (segments.Length == 1 && Path.GetExtension(entry.Name).EqualsNoCase(".nuspec"))
yield return ConvertSpecification(inputZip, entry);
else if (segments.Length >= 2 && segments[0].Equals("lib", StringComparison.OrdinalIgnoreCase))
if ((content = ConvertAssembly(segments, inputZip, entry)) != null)
yield return content;
}
}
}
finally
{
if (temporaryFile != null)
{
nuPackPackage.Close();
File.Delete(temporaryFile);
}
}
}
示例8: manualInstallMod
internal static void manualInstallMod()
{
if (isInstalling)
{
notifier.Notify(NotificationType.Warning, "Already downloading a mod", "Please try again after a few seconds.");
return;
}
isInstalling = true;
using (var dlg = new OpenFileDialog()
{
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "zip",
Filter = "Zip Files|*.zip",
FilterIndex = 1,
Multiselect = false,
Title = "Choose the mod zip to install"
})
{
//user pressed ok
if (dlg.ShowDialog() == DialogResult.OK)
{
//open the file in a stream
using (var fileStream = dlg.OpenFile())
{
ZipFile zip = new ZipFile(fileStream);
//check integrity
if (zip.TestArchive(true))
{
//look for the map file. It contains the mod name
ZipEntry map = zip.Cast<ZipEntry>().FirstOrDefault(a => a.Name.ToLower().EndsWith(".bsp"));
if (map != null)
{
//look for the version file
int entry = zip.FindEntry("addoninfo.txt", true);
if (entry >= 0)
{
string allText = string.Empty;
using (var infoStream = new StreamReader(zip.GetInputStream(entry)))
allText = infoStream.ReadToEnd();
string version = modController.ReadAddonVersion(allText);
if (!string.IsNullOrEmpty(version))
{
Version v = new Version(version);
string name = Path.GetFileNameWithoutExtension(map.Name).ToLower();
//check if this same mod is already installed and if it needs an update
if (modController.clientMods.Any(
a => a.name.ToLower().Equals(name) && new Version(a.version) >= v))
{
MessageBox.Show("The mod you are trying to install is already installed or outdated.", "Mod Manual Install",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
string targetDir = Path.Combine(d2mpDir, name);
if (Directory.Exists(targetDir))
Directory.Delete(targetDir, true);
//Make the dir again
Directory.CreateDirectory(targetDir);
if (UnzipWithTemp(null, fileStream, targetDir))
{
refreshMods();
log.Info("Mod manually installed!");
notifier.Notify(NotificationType.Success, "Mod installed", "The following mod has been installed successfully: " + name);
var mod = new ClientMod() {name = name, version = v.ToString()};
var msg = new OnInstalledMod() {Mod = mod};
Send(JObject.FromObject(msg).ToString(Formatting.None));
var existing = modController.clientMods.FirstOrDefault(m => m.name == mod.name);
if (existing != null) modController.clientMods.Remove(existing);
modController.clientMods.Add(mod);
}
else
{
MessageBox.Show("The mod could not be installed. Read the log file for details.", "Mod Manual Install",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
else
{
MessageBox.Show("Could not read the mod version from the zip file.", "Mod Manual Install",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
else
{
MessageBox.Show("No mod info was found in the zip file.", "Mod Manual Install",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//.........这里部分代码省略.........
示例9: GetZipArchiveCreateDate
private DateTimeOffset GetZipArchiveCreateDate(Stream stream)
{
var zip = new ZipFile(stream);
return zip.Cast<ZipEntry>()
.Where(f => f.Name.EndsWith(".nuspec"))
.Select(f => f.DateTime)
.FirstOrDefault();
}
示例10: LoadDescriptor
void LoadDescriptor()
{
using (Stream zipStream = PackageFile.OpenRead())
using (var zip = new ZipFile(zipStream))
{
var entries = zip.Cast<ZipEntry>();
ZipEntry descriptorFile = entries.FirstOrDefault(x => x.Name.EndsWithNoCase(".wrapdesc"));
if (descriptorFile == null)
return;
ZipEntry versionFile = entries.SingleOrDefault(x => x.Name.EqualsNoCase("version"));
SemanticVersion versionFromVersionFile = versionFile != null ? zip.Read(versionFile, x => x.ReadString().Replace("\r","").Replace("\n", "").ToSemVer()) : null;
_descriptor = zip.Read(descriptorFile, x => new PackageDescriptorReader().Read(x));
_semver = _descriptor.SemanticVersion ?? versionFromVersionFile ?? _descriptor.Version.ToSemVer();
}
}
示例11: ExtractCkanInfo
/// <summary>
/// Given a zip filename, finds and returns the first embedded .ckan file it finds.
/// Throws a MetadataNotFoundKraken if there is none.
/// </summary>
private static JObject ExtractCkanInfo(string filename)
{
using (var zipfile = new ZipFile(filename))
{
// Skip everything but embedded .ckan files.
var entries = zipfile.Cast<ZipEntry>().Where(entry => Regex.IsMatch(entry.Name, ".CKAN$", RegexOptions.IgnoreCase));
foreach (ZipEntry entry in entries)
{
log.DebugFormat("Reading {0}", entry.Name);
using (Stream zipStream = zipfile.GetInputStream(entry))
{
JObject meta_ckan = DeserializeFromStream(zipStream);
zipStream.Close();
return meta_ckan;
}
}
}
// No metadata found? Uh oh!
throw new MetadataNotFoundKraken(filename);
}
示例12: InternalOpenFile
protected Stream InternalOpenFile(string path)
{
var archive = new ZipFile(FilePath);
var entry = archive.Cast<ZipEntry> ().Single(a=>a.Name == path);
if (entry == null)
{
throw new FileNotFoundException("File does not exist.");
}
var eventStream = new CloseEventStream(archive.GetInputStream(entry));
eventStream.Closed += (s, e) => archive.Close ();
return eventStream;
}
示例13: GetFileSize
public long GetFileSize(string path)
{
var archive = new ZipFile(FilePath);
var entry = archive.Cast<ZipEntry>().Where(a=>a.Name.StartsWith("data/")).Single(a => "." + a.Name.Substring(0, a.Name.LastIndexOf(".")).Substring(a.Name.IndexOf("/")) == path);
if (entry == null)
{
throw new FileNotFoundException("File does not exist.");
}
return entry.Size;
}
示例14: GetInternalAvc
/// <summary>
/// Locates a version file in the zipfile specified, and returns an AVC object.
/// This requires a module object as we *first* search files we might install,
/// falling back to a search of all files in the archive.
///
/// Returns null if no version is found.
/// Throws a Kraken if too many versions are found.
/// </summary>
private static AvcVersion GetInternalAvc(CkanModule module, ZipFile zipfile, string internalFilePath)
{
Log.DebugFormat("Finding AVC .version file for {0}", module);
const string versionExt = ".version";
// Get all our version files.
var files = ModuleInstaller.FindInstallableFiles(module, zipfile, null)
.Select(x => x.source)
.Where(source => source.Name.EndsWith(versionExt))
.ToList();
if (files.Count == 0)
{
// Oh dear, no version file at all? Let's see if we can find *any* to use.
var versionFiles = zipfile.Cast<ZipEntry>().Where(file => file.Name.EndsWith(versionExt));
files.AddRange(versionFiles);
// Okay, there's *really* nothing there.
if (files.Count == 0)
{
return null;
}
}
var remoteIndex = 0;
if (!string.IsNullOrWhiteSpace(internalFilePath))
{
remoteIndex = -1;
for (var i = 0; i < files.Count; i++)
{
if (files[i].Name == internalFilePath)
{
remoteIndex = i;
break;
}
}
if (remoteIndex == -1)
{
var remotes = files.Aggregate("", (current, file) => current + (file.Name + ", "));
throw new Kraken(string.Format("AVC: Invalid path to remote {0}, doesn't match any of: {1}",
internalFilePath,
remotes
));
}
}
else if (files.Count > 1)
{
throw new Kraken(
string.Format("Too may .version files located: {0}",
string.Join(", ", files.Select(x => x.Name))));
}
Log.DebugFormat("Using AVC data from {0}", files[remoteIndex].Name);
// Hooray, found our entry. Extract and return it.
using (var zipstream = zipfile.GetInputStream(files[remoteIndex]))
using (var stream = new StreamReader(zipstream))
{
var json = stream.ReadToEnd();
Log.DebugFormat("Parsing {0}", json);
return JsonConvert.DeserializeObject<AvcVersion>(json);
}
}
示例15: IsResource
protected internal static bool IsResource(ZipFile zFile, string entryPath)
{
return entryPath.Equals("/") || zFile.Cast<ZipEntry>().Any(entry => entry.IsDirectory && entry.Name == entryPath);
}