Ejemplo n.º 1
0
        static FileMapping[] GetFileMaps(ICSharpCode.SharpZipLib.Zip.ZipFile zipArchive)
        {
            try
            {
                var entry = zipArchive.Cast <ZipEntry>().FirstOrDefault(f => f.Name.ToLower() == "config.json");

                if (entry != null)
                {
                    var buffer = new byte[entry.Size];

                    using (var stream = zipArchive.GetInputStream(entry))
                    {
                        StreamUtils.ReadFully(stream, buffer);
                    }

                    var json    = Encoding.UTF8.GetString(buffer);
                    var jObject = JsonHelper.DeserializeJObject(json);

                    if (jObject.TryGetValue("mappings", out var obj))
                    {
                        return(obj.ToObject <FileMapping[]>());
                    }
                }
            }
            catch (Exception)
            {
            }

            return(new FileMapping[0]);
        }
Ejemplo n.º 2
0
		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/")));
		}
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
 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);
     }
 }
Ejemplo n.º 6
0
 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)));
 }
Ejemplo n.º 7
0
        public static void UncompressZip(string archiveFile, string directory)
        {
            Log.Information("Uncompressing {File} to {Directory} ...", Path.GetFileName(archiveFile), directory);

            using var fileStream = File.OpenRead(archiveFile);
            using var zipFile    = new ICSharpCode.SharpZipLib.Zip.ZipFile(fileStream);

            var entries = zipFile.Cast <ZipEntry>().Where(x => !x.IsDirectory);

            foreach (var entry in entries)
            {
                var file = PathConstruction.Combine(directory, entry.Name);
                FileSystemTasks.EnsureExistingParentDirectory(file);

                using var entryStream  = zipFile.GetInputStream(entry);
                using var outputStream = File.Open(file, FileMode.Create);
                entryStream.CopyTo(outputStream);
            }
        }
Ejemplo n.º 8
0
        public static void UncompressZip(string archiveFile, string directory)
        {
            using (var fileStream = File.OpenRead(archiveFile))
                using (var zipFile = new ICSharpCode.SharpZipLib.Zip.ZipFile(fileStream))
                {
                    var entries = zipFile.Cast <ZipEntry>().Where(x => !x.IsDirectory);
                    foreach (var entry in entries)
                    {
                        var file = PathConstruction.Combine(directory, entry.Name);
                        FileSystemTasks.EnsureExistingParentDirectory(file);

                        using (var entryStream = zipFile.GetInputStream(entry))
                            using (var outputStream = File.Open(file, FileMode.Create))
                            {
                                entryStream.CopyTo(outputStream);
                            }
                    }
                }

            Logger.Info($"Uncompressed '{archiveFile}' to '{directory}'.");
        }
Ejemplo n.º 9
0
        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;
        }
Ejemplo n.º 10
0
        internal ArcFile OpenZipArchive(ArcView file, Stream input)
        {
            SharpZip.ZipStrings.CodePage = Properties.Settings.Default.ZIPEncodingCP;
            var zip = new SharpZip.ZipFile(input);

            try
            {
                var  files         = zip.Cast <SharpZip.ZipEntry>().Where(z => !z.IsDirectory);
                bool has_encrypted = files.Any(z => z.IsCrypted);
                if (has_encrypted)
                {
                    zip.Password = QueryPassword(file);
                }
                var dir = files.Select(z => new ZipEntry(z) as Entry).ToList();
                return(new PkZipArchive(file, this, dir, zip));
            }
            catch
            {
                zip.Close();
                throw;
            }
        }
Ejemplo n.º 11
0
        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);
                }
            }
        }
Ejemplo n.º 12
0
        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;
        }
        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();
        }
Ejemplo n.º 14
0
        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();

            }
        }
Ejemplo n.º 15
0
        /// <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);
        }
Ejemplo n.º 16
0
        /// <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);
            }
        }
Ejemplo n.º 17
0
        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;
        }
Ejemplo n.º 18
0
 protected internal static bool IsResource(ZipFile zFile, string entryPath)
 {
   return entryPath.Equals("/") || zFile.Cast<ZipEntry>().Any(entry => entry.IsDirectory && entry.Name == entryPath);
 }
Ejemplo n.º 19
0
        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);
                                }
                            }
                            else
                            {
                                MessageBox.Show("No mod map was found in the zip file.", "Mod Manual Install",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        else
                        {
                            MessageBox.Show("The zip file you selected seems to be invalid.", "Mod Manual Install",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
            }

            isInstalling = false;
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Given a zipfile, returns a `file` ModuleInstallDescriptor that can be used for
        /// installation.
        /// Returns `this` if already of a `file` type.
        /// </summary>
        public ModuleInstallDescriptor ConvertFindToFile(ZipFile zipfile)
        {
            // If we're already a file type stanza, then we have nothing to do.
            if (this.file != null)
            {
                return this;
            }

            var stanza = (ModuleInstallDescriptor) this.Clone();

            // Candidate top-level directories.
            var candidate_set = new HashSet<string>();

            // Match *only* things with our find string as a directory.
            // We can't just look for directories, because some zipfiles
            // don't include entries for directories, but still include entries
            // for the files they contain.

            string filter = @"(?:^|/)" + Regex.Escape(this.find) + @"$";

            // Let's find that directory

            // Normalise our path.
            var normalised = zipfile.Cast<ZipEntry>().Select(entry => Path.GetDirectoryName(entry.Name))
                .Select(directory =>
                {
                    var dir = directory.Replace('\\', '/');
                    return Regex.Replace(dir, "/$", "");
                });

            // If this looks like what we're after, remember it.
            var directories = normalised.Where(directory => Regex.IsMatch(directory, filter, RegexOptions.IgnoreCase));
            candidate_set.UnionWith(directories);

            // Sort to have shortest first. It's not *quite* top-level directory order,
            // but it's good enough for now.
            var candidates = new List<string>(candidate_set);
            candidates.Sort((a,b) => a.Length.CompareTo(b.Length));

            if (candidates.Count == 0)
            {
                throw new FileNotFoundKraken(
                    this.find,
                    String.Format("Could not find {0} directory in zipfile to install", this.find)
                );
            }

            // Fill in our stanza, and remove our old `find` info.
            stanza.file = candidates[0];
            stanza.find = null;
            return stanza;
        }
Ejemplo n.º 21
0
        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.EndsWith(".wrapdesc"));
                if (descriptorFile == null)
                    throw new InvalidOperationException(String.Format("The package '{0}' doesn't contain a valid .wrapdesc file.", PackageFile.Name));

                ZipEntry versionFile = entries.SingleOrDefault(x => x.Name.EqualsNoCase("version"));
                Version versionFromVersionFile = versionFile != null ? zip.Read(versionFile, x => x.ReadString().ToVersion()) : null;
                var descriptor = zip.Read(descriptorFile, x => new PackageDescriptorReader().Read(x));

                _descriptor = new DefaultPackageInfo(PackageFile.Name, versionFromVersionFile, descriptor);

                if (Descriptor.Version == null)
                    throw new InvalidOperationException("The package '{0}' doesn't have a valid version, looked in the 'wrapdesc' file, in 'version' and in the package file-name.");
            }
        }
Ejemplo n.º 22
0
 // TODO: once zip OpenFileSystem support has been implemented, replace the WrapDescriptorParser to be able to parse the directory name, version and version header in the correct precedence order, for use in other repositories.
 void LoadDescriptor()
 {
     using(var zipStream = _wrapFile.OpenRead())
     using (var zip = new ZipFile(zipStream))
     {
         var entries = zip.Cast<ZipEntry>();
         var descriptor = entries.FirstOrDefault(x => x.Name.EndsWith(".wrapdesc"));
         if (descriptor == null)
             throw new InvalidOperationException(string.Format("The package '{0}' doesn't contain a valid .wrapdesc file.", _wrapFile.Name));
         using (var stream = zip.GetInputStream(descriptor))
             Descriptor = new WrapDescriptorParser().ParseFile(new ZipWrapperFile(zip, descriptor), stream);
         if (Descriptor.Version == null)
         {
             var versionFile = entries.SingleOrDefault(x => string.Compare(x.Name, "version", StringComparison.OrdinalIgnoreCase) == 0);
             if (versionFile == null)
             {
                 Descriptor.Version = WrapNameUtility.GetVersion(this._wrapFile.NameWithoutExtension);
             }
             else
             {
                 using (var versionStream = zip.GetInputStream(versionFile))
                     Descriptor.Version = new Version(versionStream.ReadString(Encoding.UTF8));
             }
         }
         if (Descriptor.Version == null)
             throw new InvalidOperationException("The pacakge '{0}' doesn't have a valid version, looked in the 'wrapdesc' file, in 'version' and in the package file-name.");
     }
 }