コード例 #1
0
ファイル: NuGetHelpers.cs プロジェクト: ymalich/il-repack
        public static IObservable <Tuple <string, Func <Stream> > > GetNupkgContentAsync(Package package)
        {
            var o = CreateDownloadObservable(new Uri($"https://www.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 = z.Cast <ZipEntry>().ToObservable()
                              .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)
                               .Replace("%2B", "+"),
                               () => t.Item1.GetInputStream(t.Item2))));
        }
コード例 #2
0
ファイル: ZipFileSystem.cs プロジェクト: shoshiiran/cscore
        protected override IEnumerable <UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget)
        {
            var search = SearchPattern.Parse(ref path, ref searchPattern);

            return(zip.Cast <ZipEntry>().Map(x => {
                if (searchTarget == SearchTarget.File && !x.IsFile)
                {
                    return null;
                }
                if (searchTarget == SearchTarget.Directory && !x.IsDirectory)
                {
                    return null;
                }
                UPath child = UPath.DirectorySeparator + x.Name;
                if (!child.FullName.StartsWith(path.FullName))
                {
                    return null;
                }
                if (searchOption == SearchOption.TopDirectoryOnly)
                {
                    child = child.RemovePrefix(path).GetFirstPart();
                }
                if (!search.Match(child))
                {
                    return null;
                }
                return child;
            }).Filter(x => x != null).Distinct());
        }
コード例 #3
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.");
                    }
                }
        }
コード例 #4
0
        public IFileSystem Generate(ZipFile zipFile, IDictionary <string, int> fileTypes)
        {
            _root = P4kDirectory.Root();

            var files = new List <IFile>();

            foreach (ZipEntry item in zipFile.Cast <ZipEntry>()
                     .Where(f => f.IsFile))
            {
                var directory = EnsureDirectoryExists(item.Name);
                var file      = new P4kFile(item, zipFile);

                files.Add(file);
                directory.Add(file);

                // statistics
                var extension = Path.GetExtension(file.Name)
                                .TrimStart('.');

                if (!fileTypes.ContainsKey(extension))
                {
                    fileTypes.Add(extension, 1);
                }
                else
                {
                    fileTypes[extension]++;
                }
            }

            return(new P4kFileSystem(_root, Convert.ToInt32(zipFile.Count), files));
        }
コード例 #5
0
 // @todo: that library is bad or I REALLY don't know how to use it
 protected static string getRootDirectoryName(ZipFile zipFile)
 {
     return(zipFile
            .Cast <ZipEntry>()
            .First()
            .Name.Split(new[] { '/', '\\' }, 2, StringSplitOptions.RemoveEmptyEntries)[0]);
 }
コード例 #6
0
        public static AppList LoadXmlZip([NotNull] Stream stream, [CanBeNull] string password = null)
        {
            #region Sanity checks
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            #endregion

            using (var zipFile = new ZipFile(stream)
            {
                IsStreamOwner = false, Password = password
            })
            {
                var zipEntry = zipFile.Cast <ZipEntry>().First(x => StringUtils.EqualsIgnoreCase(x.Name, "data.xml"));

                try
                {
                    return(XmlStorage.LoadXml <AppList>(zipFile.GetInputStream(zipEntry)));
                }
                catch (InvalidOperationException)
                {
                    throw new InvalidDataException(Resources.SyncServerDataDamaged);
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Install the given system image.
        /// </summary>
        public void Install(SdkSystemImage image, string archivePath)
        {
            var root   = Locations.SystemImages;
            var folder = Path.Combine(root, string.Format("android-{0}", image.ApiLevel));

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            var archive = new ZipFile(archivePath);

            foreach (var entry in archive.Cast <ZipEntry>().Where(x => x.IsFile))
            {
                var path        = Path.GetFullPath(Path.Combine(folder, entry.Name));
                var entryFolder = Path.GetDirectoryName(path);
                if (!Directory.Exists(entryFolder))
                {
                    Directory.CreateDirectory(entryFolder);
                }
                using (var stream = archive.GetInputStream(entry))
                {
                    using (var fstream = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        stream.CopyTo(fstream);
                    }
                }
            }

            // Reload now
            Reload();
        }
コード例 #8
0
ファイル: ModZipRootNormalizer.cs プロジェクト: trakos/CFAN
        private static ZipEntry getInfoJsonEntry(ZipFile zipFile, string exceptionHelperText)
        {
            ZipEntry[] infoJsonEntries = zipFile
                                         .Cast <ZipEntry>()
                                         .Where(p => p.Name.EndsWith("/info.json") || p.Name.Equals("info.json"))
                                         .ToArray();
            if (!infoJsonEntries.Any())
            {
                throw new InvalidInfoJsonInZipNormalizerKraken($"Zip file does not have info.json inside ({exceptionHelperText})");
            }
            if (infoJsonEntries.Length == 1)
            {
                return(infoJsonEntries[0]);
            }
            // now if there's more than one we will return only if there is info.json that is above all other info.jsons in directory structure
            int shortestEntryNameLength = infoJsonEntries.Min(p => p.Name.Length);

            ZipEntry[] shortestInfoJsonEntries = infoJsonEntries.Where(p => p.Name.Length == shortestEntryNameLength).ToArray();
            // same length for more than one info.json means there can't be info.json that would be above other
            if (shortestInfoJsonEntries.Length > 1)
            {
                throw new InvalidInfoJsonInZipNormalizerKraken($"Zip file has more than one info.json with same path length ({exceptionHelperText})");
            }
            // if all names starts with parent dir of first info.json we're all good
            // there is a special case where parentDirectory is just ""
            ZipEntry shortestInfoJsonEntry = shortestInfoJsonEntries.First();
            string   parentDirectory       = getParentDirectory(shortestInfoJsonEntry.Name);

            if (infoJsonEntries.All(p => p.Name.StartsWith(parentDirectory)))
            {
                return(shortestInfoJsonEntry);
            }
            throw new InvalidInfoJsonInZipNormalizerKraken($"Zip file has more than one info.json in different directories ({exceptionHelperText})");
        }
コード例 #9
0
ファイル: ModuleService.cs プロジェクト: net-lisias-kspw/CKAN
        /// <summary>
        /// Locate a version file in an archive.
        /// 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>
        /// <param name="module">The metadata associated with this module, used to find installable files</param>
        /// <param name="zipfile">The archive containing the module's files</param>
        /// <param name="internalFilePath">Filter for selecting a version file, either exact match or regular expression</param>
        /// <returns>
        /// Tuple consisting of the chosen file's entry in the archive plus a boolean
        /// indicating whether it's a file would be extracted to disk at installation
        /// </returns>
        public Tuple <ZipEntry, bool> FindInternalAvc(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 ksp   = new GameInstance(new KerbalSpaceProgram(), "/", "dummy", new NullUser());
            var files = ModuleInstaller.FindInstallableFiles(module, zipfile, ksp)
                        .Select(x => x.source)
                        .Where(source => source.Name.EndsWith(versionExt,
                                                              StringComparison.InvariantCultureIgnoreCase))
                        .ToList();
            // By default, we look for ones we can install
            var installable = true;

            if (files.Count == 0)
            {
                // Oh dear, no version file at all? Let's see if we can find *any* to use.
                files.AddRange(zipfile.Cast <ZipEntry>()
                               .Where(file => file.Name.EndsWith(versionExt,
                                                                 StringComparison.InvariantCultureIgnoreCase)));

                if (files.Count == 0)
                {
                    // Okay, there's *really* nothing there.
                    return(null);
                }
                // Tell calling code that it may not be a "real" version file
                installable = false;
            }

            if (!string.IsNullOrWhiteSpace(internalFilePath))
            {
                Regex    internalRE = new Regex(internalFilePath, RegexOptions.Compiled);
                ZipEntry avcEntry   = files
                                      .Where(f => f.Name == internalFilePath || internalRE.IsMatch(f.Name))
                                      .FirstOrDefault();
                if (avcEntry == null)
                {
                    throw new Kraken(
                              string.Format("AVC: Invalid path to remote {0}, doesn't match any of: {1}",
                                            internalFilePath,
                                            string.Join(", ", files.Select(f => f.Name))
                                            ));
                }
                return(new Tuple <ZipEntry, bool>(avcEntry, installable));
            }
            else if (files.Count > 1)
            {
                throw new Kraken(
                          string.Format("Too many .version files located: {0}",
                                        string.Join(", ", files.Select(x => x.Name))));
            }
            else
            {
                return(new Tuple <ZipEntry, bool>(files.First(), installable));
            }
        }
コード例 #10
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());
        }
コード例 #11
0
        private static ZipFile PurgeCachedExtractContent(ZipFile zipFile)
        {
            foreach (ZipEntry cachedExtractContentEntry in zipFile.Cast <ZipEntry>().Where(entry => entry.Name.StartsWith("TwbxExternalCache")))
            {
                zipFile.Delete(cachedExtractContentEntry);
            }

            return(zipFile);
        }
コード例 #12
0
        public static void ExtractZipFile(string archiveFilenameIn, string password, string outFolder)
        {
            ZipFile zf = null;

            try
            {
                using (var fs = File.OpenRead(archiveFilenameIn))
                {
                    zf = new ZipFile(fs);
                    if (!IsNullOrEmpty(password))
                    {
                        zf.Password = password;     // AES encrypted entries are handled automatically
                    }
                    foreach (var zipEntry in zf.Cast <ZipEntry>().Where(zipEntry => zipEntry.IsFile))
                    {
                        var entryFileName = zipEntry.Name;
                        // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
                        // Optionally match entrynames against a selection list here to skip as desired.
                        // The unpacked length is available in the zipEntry.Size property.

                        var buffer    = new byte[4096];  // 4K is optimum
                        var zipStream = zf.GetInputStream(zipEntry);

                        // Manipulate the output filename here as desired.
                        var fullZipToPath = Path.Combine(outFolder, entryFileName);
                        var directoryName = Path.GetDirectoryName(fullZipToPath);
                        if (!IsNullOrEmpty(directoryName))
                        {
                            Directory.CreateDirectory(directoryName);
                        }

                        // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
                        // of the file, but does not waste memory.
                        // The "using" will close the stream even if an exception occurs.
                        using (var streamWriter = File.Create(fullZipToPath))
                        {
                            StreamUtils.Copy(zipStream, streamWriter, buffer);
                        }
                    }
                }
            }
            finally
            {
                if (zf != null)
                {
                    zf.IsStreamOwner = true; // Makes close also shut the underlying stream
                    zf.Close();              // Ensure we release resources
                }

                //Delete zip downloaded zip
                if (File.Exists(archiveFilenameIn))
                {
                    File.Delete($"{archiveFilenameIn}");
                }
            }
        }
コード例 #13
0
ファイル: Reader.cs プロジェクト: ruo2012/DocxToText
        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);
        }
コード例 #14
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);
        }
コード例 #15
0
 protected override IEnumerable <TreeNodeVM> LoadChildren()
 {
     using (var stream = File.OpenRead(this.Path))
     {
         using (var zipFile = new ZipFile(stream))
         {
             var entries = zipFile.Cast <ZipEntry>();
             return(this.GenerateChildren(entries));
         }
     }
 }
コード例 #16
0
        public IFileSystem Generate(ZipFile zipFile)
        {
            _root = P4kDirectory.Root();
            foreach (ZipEntry item in zipFile.Cast <ZipEntry>()
                     .Where(f => f.IsFile))
            {
                var directory = EnsureDirectoryExists(item.Name);
                directory.Add(new P4kFile(item, zipFile));
            }

            return(new P4kFileSystem(_root));
        }
コード例 #17
0
ファイル: WriterChunk.cs プロジェクト: radtek/DocsUploader
        private void bkg_Archive(object filenamePair)
        {
            KeyValuePair <string, string> pair = (KeyValuePair <string, string>)filenamePair;
            string fileName     = pair.Key;
            string newFileName  = pair.Value;
            string zipfileName  = Path.ChangeExtension(fileName, "zip");
            string zipEntryName = Path.GetFileName(newFileName);

            lock (archiveSyncLock)
            {
                zipEntryName = GetNewZipEntryFileName(fileName, zipEntryName, 1, zipfileName, null);
                Dictionary <string, string> zipItems = new Dictionary <string, string>();
                if (File.Exists(zipfileName))
                {
                    ZipFile zip = new ZipFile(zipfileName);
                    IEnumerable <ZipEntry> zes = zip.Cast <ZipEntry>();
                    foreach (ZipEntry zie in zes)
                    {
                        zipItems.Add(
                            zie.Name,
                            new StreamReader(
                                zip.GetInputStream(zie)).ReadToEnd());
                    }
                    zip.Close();
                }
                FileStream      fs  = File.Open(zipfileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                ZipOutputStream zos = new ZipOutputStream(fs);
                //Считываем все из старого файла
                byte[] content = File.ReadAllBytes(newFileName);
                //Создаем файлы в архиве
                foreach (var item in zipItems)
                {
                    ZipEntry ze = new ZipEntry(item.Key);
                    zos.PutNextEntry(ze);
                    byte[] itemContent = Encoding.Default.GetBytes(item.Value);
                    zos.Write(itemContent, 0, itemContent.Length);
                }
                ZipEntry zipe = new ZipEntry(zipEntryName);
                zos.PutNextEntry(zipe);
                zos.Write(content, 0, content.Length);
                zos.Flush();
                zos.Close();
                zos.Dispose();
                fs.Close();
                fs.Dispose();
                if (File.Exists(newFileName))
                {
                    File.Delete(newFileName);
                }
            }
        }
コード例 #18
0
ファイル: ModuleInstaller.cs プロジェクト: skaven81/CKAN
        /// <summary>
        /// Returns a default install stanza for the module provided. This finds the topmost
        /// directory which matches the module identifier, and generates a stanza that
        /// installs that into GameData.
        ///
        /// Throws a FileNotFoundKraken() if unable to locate a suitable directory.
        /// </summary>
        internal static ModuleInstallDescriptor GenerateDefaultInstall(string identifier, ZipFile zipfile)
        {
            var stanza = new ModuleInstallDescriptor {
                install_to = "GameData"
            };

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

            // Match *only* things with our module identifier 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 ident_filter = @"(?:^|/)" + Regex.Escape(identifier) + @"$";

            // 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, ident_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(
                          identifier,
                          String.Format("Could not find {0} directory in zipfile to install", identifier)
                          );
            }

            // Fill in our stanza!
            stanza.file = candidates[0];
            return(stanza);
        }
コード例 #19
0
        private List <ExtractFileInfo> BuildExtractFileListFromPaths()
        {
            _progressController.SetIndeterminate();

            var destinationRoot = this.GetDestinationRoot(this.LocalPath); // the root directory of output

            var zipFile = new ZipFile(File.OpenRead(this.PackagePath));
            var entries = zipFile.Cast <ZipEntry>().Where(entry => entry.Name.StartsWith(this.LocalPath)).ToArray();

            var fileList = new List <ExtractFileInfo>();

            foreach (var entry in entries)
            {
                if (_progressController.IsCanceled)
                {
                    _task = _task.ContinueWith(t => _progressController.CloseAsync().Wait())
                            .ContinueWith(t => this.EndAndDispose(this.L("package_support", "extract_state_cancelled")));
                    return(null);
                }

                string relativePath;
                if (this.UseRelativePath)
                {
                    relativePath = PathEx.Relativize(entry.Name, this.LocalPath);

                    if (relativePath[0] == '/')
                    {
                        relativePath = relativePath.Substring(1);
                    }
                }
                else
                {
                    relativePath = entry.Name;
                }

                var fileInfo = new ExtractFileInfo();
                fileInfo.Entry           = entry;
                fileInfo.ZipFile         = zipFile;
                fileInfo.DestinationRoot = destinationRoot;
                fileInfo.RelativePath    = relativePath;
                fileInfo.PackagePath     = this.PackagePath;

                fileList.Add(fileInfo);
            }

            return(fileList);
        }
コード例 #20
0
ファイル: AVC.cs プロジェクト: skaven81/CKAN
        /// <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>
        public static AVC FromZipFile(CkanModule module, ZipFile zipfile)
        {
            log.DebugFormat("Finding AVC .version file for {0}", module);

            const string version_ext = ".version";

            // Get all our version files.
            List <ZipEntry> files = ModuleInstaller.FindInstallableFiles(module, zipfile, null)
                                    .Select(x => x.source)
                                    .Where(source => source.Name.EndsWith(version_ext))
                                    .ToList();

            if (files.Count == 0)
            {
                // Oh dear, no version file at all? Let's see if we can find *any* to use.
                var version_files = zipfile.Cast <ZipEntry>().Where(file => file.Name.EndsWith(version_ext));
                files.AddRange(version_files);

                // Okay, there's *really* nothing there.
                if (files.Count == 0)
                {
                    return(null);
                }
            }

            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[0].Name);

            // Hooray, found our entry. Extract and return it.
            using (var zipstream = zipfile.GetInputStream(files[0]))
                using (StreamReader stream = new StreamReader(zipstream))
                {
                    string json = stream.ReadToEnd();

                    log.DebugFormat("Parsing {0}", json);
                    return(JsonConvert.DeserializeObject <AVC>(json));
                }
        }
コード例 #21
0
ファイル: ZipFilePackage.cs プロジェクト: llenroc/openwrap
        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();
                }
        }
コード例 #22
0
        public static IEnumerable <PackageContent> Content(Stream nuGetPackage)
        {
            PackageContent content       = null;
            string         temporaryFile = null;

            try
            {
                if (!nuGetPackage.CanSeek)
                {
                    temporaryFile = Path.GetTempFileName();
                    using (var temporaryFileStream = File.OpenWrite(temporaryFile))
                        nuGetPackage.CopyTo(temporaryFileStream);

                    nuGetPackage = File.OpenRead(temporaryFile);
                }
                using (var inputZip = new ZipFile(nuGetPackage))
                {
                    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].EqualsNoCase("lib"))
                        {
                            if ((content = ConvertAssembly(segments, inputZip, entry)) != null)
                            {
                                yield return(content);
                            }
                        }
                    }
                }
            }
            finally
            {
                if (temporaryFile != null)
                {
                    nuGetPackage.Close();
                    File.Delete(temporaryFile);
                }
            }
        }
コード例 #23
0
        public IEnumerator <VfsEntry> GetEnumerator(VfsEntry entry)
        {
            // adapter on top of ZipFile enumerator to filter only current directory
            return(_zipFile.Cast <ZipEntry>()
                   .Where((ze) => {
                // if it does not start with the current subdir, skip it
                if (!ze.Name.StartsWith(_pathInZip))
                {
                    return false;
                }
                // if its name starts with the path and is the same length, it is the same string
                // but we only want children, not the directory itself.
                if (ze.Name.Length == _pathInZip.Length)
                {
                    return false;
                }
                var firstSlash = ze.Name.IndexOf(System.IO.Path.DirectorySeparatorChar, _pathInZip.Length);
                // if we don't find any subsequent slashes, it has to be a file in the current subdir
                if (firstSlash == -1)
                {
                    return true;
                }
                // if we find a slash at the end of the entrys name it is a subdirectory in the current directory
                if (firstSlash == ze.Name.Length - 1)
                {
                    return true;
                }
                // otherwise it is something further down in the tree which we don't want here
                return false;
            })

                   .Select((e) => {
                if (e.IsDirectory)
                {
                    return new VfsEntry(Path.GetFileName(e.Name.Remove(e.Name.Length - 1)), false, 0, entry, new VfsZipDirectory(_zipFile, e.Name));
                }
                else
                {
                    return new VfsEntry(Path.GetFileName(e.Name), e.IsFile, e.Size, entry);
                }
            }).GetEnumerator());
        }
コード例 #24
0
        private static void ExtractPackageFiles()
        {
            var executingAssembly    = Assembly.GetEntryAssembly().NotNull();
            var packageResourceNames = executingAssembly.GetManifestResourceNames().Where(x => x.EndsWithOrdinalIgnoreCase("nupkg")).ToList();

            if (!packageResourceNames.Any())
            {
                return;
            }

            var globalPackagesDirectory = Constants.GlobalNukeDirectory / "packages";

            Log.Information("Extracting packages to {PackagesDirectory}", globalPackagesDirectory);

            foreach (var packageResourceName in packageResourceNames)
            {
                Log.Verbose("Extracting {Package}", packageResourceName);

                var packageResourceStream = executingAssembly.GetManifestResourceStream(packageResourceName).NotNull();
                var packageArchiveReader  = new PackageArchiveReader(packageResourceStream);
                var nuspecReader          = packageArchiveReader.NuspecReader;
                var packageFile           = globalPackagesDirectory / nuspecReader.GetId() / nuspecReader.GetVersion().ToString() / packageResourceName;

                packageResourceStream.Seek(offset: 0, SeekOrigin.Begin);
                packageResourceStream.CopyToFile(packageFile);

                using var fileStream = File.OpenRead(packageFile);
                using var zipFile    = new ZipFile(fileStream);

                var entries = zipFile.Cast <ZipEntry>().Where(x => !x.IsDirectory);
                foreach (var entry in entries)
                {
                    var file = packageFile.Parent / entry.Name;
                    Directory.CreateDirectory(file.Parent);

                    using var entryStream  = zipFile.GetInputStream(entry);
                    using var outputStream = File.Open(file, FileMode.Create);
                    entryStream.CopyTo(outputStream);
                }
            }
        }
コード例 #25
0
ファイル: ParserTests.cs プロジェクト: sq/ILSpy-JSIL
        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/")));
        }
コード例 #26
0
        /// <summary>
        /// Extracts a .zip archive and tallies the work done.
        /// </summary>
        protected UnzipResult ExtractZip(Stream zipStream, string zipName, string destinationDirectory)
        {
            ZipFile zipFile = new ZipFile(zipStream)
            {
                IsStreamOwner = true
            };

            try
            {
                UnzipResult result = ExtractZipContents(zipFile, zipName, destinationDirectory);
                result.CompressedSize = zipFile.Cast <ZipEntry>().Sum(zipEntry => zipEntry.CompressedSize);

                Log.InfoFormat("Extracted {0} files from '{1}'. ({2} unpacked)", result.ExtractedFileCount, zipName, result.ExtractedSize.ToPrettySize(decimalPlaces: 1));

                return(result);
            }
            finally
            {
                zipFile.Close();
            }
        }
コード例 #27
0
ファイル: MainClass.cs プロジェクト: pjf/CKAN
        /// <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);
        }
コード例 #28
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);
        }
コード例 #29
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 => entry.Name.EndsWith(".ckan",
                                                                  StringComparison.InvariantCultureIgnoreCase));

                foreach (var entry in entries)
                {
                    Log.DebugFormat("Reading {0}", entry.Name);

                    using (var zipStream = zipfile.GetInputStream(entry))
                    {
                        return(DeserializeFromStream(zipStream));
                    }
                }
            }

            return(null);
        }
コード例 #30
0
        private void BackgroundWorker_Extractor_DoWork(object sender, System.ComponentModel.DoWorkEventArgs args)
        {
            using (var fs = TempFile.Open(PCLExt.FileStorage.FileAccess.Read))
                using (var zip = new ZipFile(fs))
                {
                    var result = zip.Cast <ZipEntry>().Skip(1).Where(entry => entry.Name.Contains(zip[0].Name)).ToList(); // -- Zip should contain main folder.

                    // -- We skip the Main folder.
                    for (var i = 0; i < result.Count; i++)
                    {
                        if (args.Cancel)
                        {
                            return;
                        }

                        var zipEntry = result[i];
                        var path     = zipEntry.Name.Replace(zip[0].Name, "");

                        if (zipEntry.IsDirectory)
                        {
                            ExtractionFolder.GetFolderFromPath(path);
                        }
                        if (zipEntry.IsFile)
                        {
                            using (var inputStream = zip.GetInputStream(zipEntry))
                                using (var fileStream = ExtractionFolder.GetFileFromPath(path).Open(PCLExt.FileStorage.FileAccess.ReadAndWrite))
                                    inputStream.CopyTo(fileStream);
                        }

                        double bytesIn    = i;
                        double totalBytes = result.Count;
                        double percentage = bytesIn / totalBytes * 100;
                        BackgroundWorker_Extractor.ReportProgress((int)percentage);
                    }
                }
        }