Exemple #1
0
 /// <summary>
 /// Creates a file on the file system with the entry?s contents and the specified name. The last write time of the file is set to the
 /// entry?s last write time. This method does not allow overwriting of an existing file with the same name. Attempting to extract explicit
 /// directories (entries with names that end in directory separator characters) will not result in the creation of a directory.
 /// </summary>
 ///
 /// <exception cref="UnauthorizedAccessException">The caller does not have the required permission.</exception>
 /// <exception cref="ArgumentException">destinationFileName is a zero-length string, contains only white space, or contains one or more
 /// invalid characters as defined by InvalidPathChars. -or- destinationFileName specifies a directory.</exception>
 /// <exception cref="ArgumentNullException">destinationFileName is null.</exception>
 /// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length.
 /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.</exception>
 /// <exception cref="DirectoryNotFoundException">The path specified in destinationFileName is invalid (for example, it is on
 /// an unmapped drive).</exception>
 /// <exception cref="IOException">destinationFileName already exists.
 /// -or- An I/O error has occurred. -or- The entry is currently open for writing.
 /// -or- The entry has been deleted from the archive.</exception>
 /// <exception cref="NotSupportedException">destinationFileName is in an invalid format
 /// -or- The ZipArchive that this entry belongs to was opened in a write-only mode.</exception>
 /// <exception cref="InvalidDataException">The entry is missing from the archive or is corrupt and cannot be read
 /// -or- The entry has been compressed using a compression method that is not supported.</exception>
 /// <exception cref="ObjectDisposedException">The ZipArchive that this entry belongs to has been disposed.</exception>
 ///
 /// <param name="destinationFile">The name of the file that will hold the contents of the entry.
 /// The path is permitted to specify relative or absolute path information.
 /// Relative path information is interpreted as relative to the current working directory.</param>
 public static void ExtractToFile(this ZipArchiveEntry source, FileEntry destinationFile)
 {
     ExtractToFile(source, destinationFile, false);
 }
Exemple #2
0
        public static async Task <EpubPackage> ReadPackageAsync(ZipArchive epubArchive, string rootFilePath)
        {
            ZipArchiveEntry rootFileEntry = epubArchive.GetEntry(rootFilePath);

            if (rootFileEntry == null)
            {
                throw new Exception("EPUB parsing error: root file not found in archive.");
            }
            XDocument containerDocument;

            using (Stream containerStream = rootFileEntry.Open())
            {
                containerDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false);
            }
            XNamespace  opfNamespace     = "http://www.idpf.org/2007/opf";
            XElement    packageNode      = containerDocument.Element(opfNamespace + "package");
            EpubPackage result           = new EpubPackage();
            string      epubVersionValue = packageNode.Attribute("version").Value;

            if (epubVersionValue == "2.0")
            {
                result.EpubVersion = EpubVersion.EPUB_2;
            }
            else if (epubVersionValue == "3.0")
            {
                result.EpubVersion = EpubVersion.EPUB_3;
            }
            else
            {
                throw new Exception(String.Format("Unsupported EPUB version: {0}.", epubVersionValue));
            }
            XElement metadataNode = packageNode.Element(opfNamespace + "metadata");

            if (metadataNode == null)
            {
                throw new Exception("EPUB parsing error: metadata not found in the package.");
            }
            EpubMetadata metadata = ReadMetadata(metadataNode, result.EpubVersion);

            result.Metadata = metadata;
            XElement manifestNode = packageNode.Element(opfNamespace + "manifest");

            if (manifestNode == null)
            {
                throw new Exception("EPUB parsing error: manifest not found in the package.");
            }
            EpubManifest manifest = ReadManifest(manifestNode);

            result.Manifest = manifest;
            XElement spineNode = packageNode.Element(opfNamespace + "spine");

            if (spineNode == null)
            {
                throw new Exception("EPUB parsing error: spine not found in the package.");
            }
            EpubSpine spine = ReadSpine(spineNode);

            result.Spine = spine;
            XElement guideNode = packageNode.Element(opfNamespace + "guide");

            if (guideNode != null)
            {
                EpubGuide guide = ReadGuide(guideNode);
                result.Guide = guide;
            }
            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Updates the Manifest.xml file of the datapackage to be uploaded with the original file name of the related data message before the upload
        /// </summary>
        /// <param name="archive">The data package which is being prepared for upload</param>
        /// <param name="dataMessage">The data message object containing the inforation related to the file which is getting uploaded</param>
        /// <param name="fileNameInPackageOrig">Original file name in the package</param>
        /// <returns>Final file name in the package</returns>
        private string UpdateManifestFile(ZipArchive archive, DataMessage dataMessage, string fileNameInPackageOrig)
        {
            if (archive is null || dataMessage is null)
            {
                return(fileNameInPackageOrig);
            }

            // This modification will cause that the original file name is used in the package that is going to be uploaded to D365
            // Get the manifest.xml entery
            ZipArchiveEntry manifestEntry = archive.GetEntry("Manifest.xml");

            // Save the Manifest.xml as temporary file
            string tempManifestFileName    = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            string tempManifestFileNameNew = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            manifestEntry.ExtractToFile(tempManifestFileName, true);

            // Modify the file name to the original filename
            XmlDocument tempXmlDocManifest = new XmlDocument();

            using (var tempManifestFile = new StreamReader(tempManifestFileName))
            {
                tempXmlDocManifest.Load(new XmlTextReader(tempManifestFile)
                {
                    Namespaces = false
                });
                tempXmlDocManifest.SelectSingleNode("//InputFilePath[1]").InnerText = Path.GetFileName(dataMessage.FullPath);

                // Save the document to a file and auto-indent the output.
                using XmlTextWriter writer = new XmlTextWriter(tempManifestFileNameNew, null)
                      {
                          Namespaces = false,
                          Formatting = System.Xml.Formatting.Indented
                      };
                tempXmlDocManifest.Save(writer);
            }

            // Delete the Manifest.xml from the archive file
            manifestEntry.Delete();

            // Add a new Manifest.xml based on the adjusted file
            archive.CreateEntryFromFile(tempManifestFileNameNew, "Manifest.xml");

            // Delete the tempoirary file
            File.Delete(tempManifestFileName);
            File.Delete(tempManifestFileNameNew);

            // Adapt the fileNameInPackage
            string fileNameInPackage = Path.GetFileName(dataMessage.FullPath);

            // Check if package template contains input file and remove it first. It should not be there in the first place.
            ZipArchiveEntry entry = archive.GetEntry(fileNameInPackage);

            if (entry != null)
            {
                entry.Delete();
                Log.WarnFormat(CultureInfo.InvariantCulture, string.Format(Resources.Job_0_Package_template_contains_input_file_1_Please_remove_it_from_the_template, fileNameInPackage));
            }

            return(fileNameInPackage);
        }
Exemple #4
0
        public void Read(ZipArchive archive, string MapName, bool ApplyUpdates)
        {
            string          basename  = MapName.Remove(MapName.Length - 4);
            Stream          S57map    = null;
            ZipArchiveEntry baseentry = null;
            SortedList <uint, ZipArchiveEntry> updatefiles = new SortedList <uint, ZipArchiveEntry>();
            Stream S57update;

            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (entry.Name.Contains(basename))
                {
                    if (entry.Name.Equals(MapName))
                    {
                        baseentry = entry;
                    }
                    else
                    {
                        int    val;
                        string end = entry.Name.Substring(entry.Name.Length - 3);
                        if (char.IsDigit(end[0]) && char.IsDigit(end[1]) && char.IsDigit(end[2]))
                        {
                            int.TryParse(end, out val);
                            updatefiles.Add(Convert.ToUInt32(end.ToString()), entry);
                        }
                    }
                }
            }
            S57map = baseentry.Open();
            int count = (int)baseentry.Length;

            if (fileByteArray == null)
            {
                fileByteArray = new byte[count];
            }
            else
            {
                Array.Clear(fileByteArray, 0, fileByteArray.Length);
                Array.Resize(ref fileByteArray, count);
            }
            MemoryStream memoryStream = new MemoryStream(fileByteArray);

            S57map.CopyTo(memoryStream);
            memoryStream.Dispose();

            using (var reader = new Iso8211Reader(fileByteArray))
            {
                baseFile = new BaseFile(reader);
                foreach (var bla in reader.tagcollector)
                {
                    Console.WriteLine(bla);
                }
            }
            S57map.Dispose();

            if (ApplyUpdates)
            {
                foreach (var entry in updatefiles)
                {
                    S57update = entry.Value.Open();
                    count     = (int)entry.Value.Length;
                    Array.Clear(fileByteArray, 0, fileByteArray.Length);
                    Array.Resize(ref fileByteArray, count);
                    memoryStream = new MemoryStream(fileByteArray);
                    S57update.CopyTo(memoryStream);
                    memoryStream.Dispose();
                    using (var updatereader = new Iso8211Reader(fileByteArray))
                    {
                        UpdateFile updateFile = new UpdateFile(updatereader);
                        baseFile.ApplyUpdateFile(updateFile);
                    }
                    S57update.Dispose();
                }
            }
            cellInfo = new Cell(baseFile);
            baseFile.BindVectorPointersOfVectors();
            baseFile.BindVectorPointersOfFeatures();
            baseFile.BuildVectorGeometry();
            baseFile.BindFeatureObjectPointers();
        }
 public ParallelReadableZipArchiveEntry(ZipArchiveEntry zipArchiveEntry, ParallelReadableZipArchive parallelReadableZipArchive)
 {
     this.zipArchive    = parallelReadableZipArchive;
     this.FullName      = zipArchiveEntry.FullName;
     this.LastWriteTime = zipArchiveEntry.LastWriteTime;
 }
Exemple #6
0
        public void DeserializeFiddlerTrace(String filePath, String customAgent)
        {
            // Open the SAZ
            var archive = System.IO.Compression.ZipFile.Open(filePath, ZipArchiveMode.Read);

            // Group the archive entries by frame number
            var result = from e in archive.Entries
                         where e.Name.Contains("_c.txt") || e.Name.Contains("_s.txt") || e.Name.Contains("_m.xml")
                         group e by Utils.GetFrameNumber(e.Name) into g
                         select new { Frame = g.Key, Data = g };

            List <ServiceCallItem> frameData = new List <ServiceCallItem>();

            // Process data per frame
            foreach (var group in result)
            {
                // Grab the individual files
                ZipArchiveEntry cFileArchive = group.Data.ElementAt(0);
                ZipArchiveEntry mFileArchive = group.Data.ElementAt(1);
                ZipArchiveEntry sFileArchive = group.Data.ElementAt(2);

                ServiceCallItem frame = null;

                frame = ServiceCallItem.FromFiddlerFrame((UInt32)group.Frame, cFileArchive, mFileArchive, sFileArchive);

                // If this is not an Xbox Service Endpoint that we are checking, then move along.
                if (frame == null || (!Utils.IsAnalyzedService(frame, customAgent) && m_allEndpoints == false))
                {
                    continue;
                }

                frameData.Add(frame);
            }

            var consoleGroups = from f in frameData
                                group f by f.m_consoleIP;

            foreach (var consoleFrames in consoleGroups.Where(g => g.Key != String.Empty))
            {
                var consoleData = new PerConsoleData();

                var xboxServiceFrames = consoleFrames.GroupBy(f => f.m_host)
                                        .Select(group => new { Host = group.Key, History = group.AsEnumerable() });

                consoleData.m_servicesHistory = xboxServiceFrames.ToDictionary(g => g.Host, g => new LinkedList <ServiceCallItem>(g.History.OrderBy(call => call.m_reqTimeUTC)));

                // Xbox telemetry endpoint
                if (consoleData.m_servicesHistory.ContainsKey("data-vef.xboxlive.com"))
                {
                    ConvertCS1ToEvent(consoleData.m_servicesHistory);
                }

                // Windows Telemetry endpoint
                if (consoleData.m_servicesHistory.Any(k => k.Key.Contains(".data.microsoft.com")))
                {
                    ConvertCS2ToEvent(consoleData.m_servicesHistory);
                }

                foreach (var endpoint in consoleData.m_servicesHistory)
                {
                    consoleData.m_servicesStats.Add(endpoint.Key, new ServiceCallStats(endpoint.Value));
                }

                m_perConsoleData.Add(consoleFrames.Key, consoleData);
            }
        }
Exemple #7
0
 public static void ExtractToFile(this ZipArchiveEntry source, string destinationFileName, bool overwrite);
Exemple #8
0
        static void Main(string[] args)
        {
            string recafJarPath = Path.Combine(Directory.GetCurrentDirectory(), "recaf-edit.jar");
            string destJarPath  = Path.Combine(Directory.GetCurrentDirectory(), "spacehaven.jar");
            string destZipPath  = Path.Combine(Directory.GetCurrentDirectory(), "patch.zip");

            Console.WriteLine("Config : JarPacker.config");
            Console.WriteLine("Recaf JAR : recaf-edit.jar");
            Console.WriteLine("Destination JAR : spacehaven.jar");
            Console.WriteLine("---------------------------------------------------------");


            string[] classPaths = File.ReadAllLines(Path.Combine(Directory.GetCurrentDirectory(), "JarPacker.config"));


            using (ZipArchive recafJar = ZipFile.Open(recafJarPath, ZipArchiveMode.Read))
            {
                List <ZipArchiveEntry> classEntries = new List <ZipArchiveEntry>();
                foreach (string classPath in classPaths)
                {
                    ZipArchiveEntry classEntry = recafJar.GetEntry(classPath.Trim());
                    if (classEntry == null)
                    {
                        Console.WriteLine($"Can't find {classPath} in {recafJarPath}");
                        Console.ReadLine();
                        return;
                    }
                    classEntries.Add(classEntry);
                }

                Console.WriteLine("What do you want to do ?");
                Console.WriteLine("[1] Update spacehaven.jar");
                Console.WriteLine("[2] Create patch zip");
                switch (Console.ReadKey(true).KeyChar)
                {
                case '1':
                    using (ZipArchive destJar = ZipFile.Open(destJarPath, ZipArchiveMode.Update))
                    {
                        foreach (ZipArchiveEntry classEntry in classEntries)
                        {
                            destJar.GetEntry(classEntry.FullName).Delete();
                            ZipArchiveEntry updatedEntry = destJar.CreateEntry(classEntry.FullName);
                            classEntry.Open().CopyTo(updatedEntry.Open());
                            Console.WriteLine($"Updated {classEntry.FullName}");
                        }
                    }
                    Console.WriteLine($"Updated all classes in {destJarPath}");
                    break;

                case '2':
                    using (FileStream fileStream = new FileStream(destZipPath, FileMode.Create))
                    {
                        using (ZipArchive destZip = new ZipArchive(fileStream, ZipArchiveMode.Update))
                        {
                            foreach (ZipArchiveEntry classEntry in classEntries)
                            {
                                ZipArchiveEntry newEntry = destZip.CreateEntry(classEntry.FullName);
                                classEntry.Open().CopyTo(newEntry.Open());
                                Console.WriteLine($"Created {classEntry.FullName}");
                            }
                        }
                    }
                    Console.WriteLine($"Zip patch written : {destZipPath}");

                    break;
                }
            }

            Console.ReadLine();
        }
        /// <summary>
        /// Determines NuGet packages from 'packages.config'.
        /// </summary>
        /// <param name="project">Project to inspect.</param>
        /// <param name="packagesFolders">Possible 'packages' folders inside solution or project directories.</param>
        private void GetNuGetPackagesFromPackagesConfig(ProjectInfo project, HashSet <string> packagesFolders)
        {
            sLog.Write(LogLevel.Debug, "Get nuspec files for '{0}'", project.ProjectName);

            var packagePaths = new Dictionary <string, string>();

            // extract 'packages.config' and determine id and version
            foreach (XElement element in XElement.Load(project.NuGetInformationPath).Descendants())
            {
                switch (element.Name.LocalName)
                {
                case "package":
                    string id = element.Attribute("id").Value;
                    if (string.IsNullOrEmpty(id))
                    {
                        continue;
                    }
                    string version = element.Attribute("version").Value;
                    if (string.IsNullOrEmpty(version))
                    {
                        continue;
                    }
                    packagePaths.Add(id, $"{id}.{version}");
                    break;
                }
            }

            var nugetPackages = new Dictionary <string, string>();

            // find path to NuGet packages inside one of the packages folder
            foreach (string id in packagePaths.Keys)
            {
                foreach (string packagesFolder in packagesFolders)
                {
                    string nugetPackagePath = Path.Combine(packagesFolder, packagePaths[id], $"{packagePaths[id]}.nupkg");
                    if (!File.Exists(nugetPackagePath))
                    {
                        continue;
                    }

                    sLog.Write(LogLevel.Debug, "{0}: Found NuGet package '{1}'.", project.ProjectName, nugetPackagePath);
                    nugetPackages.Add(id, nugetPackagePath);
                    break;
                }
            }

            // extract 'nuspec' file from package and store information
            foreach (string nugetId in nugetPackages.Keys)
            {
                if (mNuGetPackages.ContainsKey(nugetId))
                {
                    // NuGet package dependency was already found within another project
                    continue;
                }

                // extract nuspec file from package
                using (var fs = new FileStream(nugetPackages[nugetId], FileMode.Open))
                {
                    var             archive = new ZipArchive(fs, ZipArchiveMode.Read);
                    ZipArchiveEntry nuSpec  = archive.Entries.First(x => x.Name.EndsWith(".nuspec"));
                    if (nuSpec == null)
                    {
                        sLog.Write(LogLevel.Error, "The NuGet package '{0}' does not contains an '.nuspec' file.", nugetId);
                        continue;
                    }

                    string nuspecPath = Path.Combine(Path.GetDirectoryName(nugetPackages[nugetId]), $"{nugetId}.nuspec");
                    using (var nuspecStream = new FileStream(nuspecPath, FileMode.Create, FileAccess.Write))
                    {
                        nuSpec.Open().CopyTo(nuspecStream);
                    }

                    sLog.Write(LogLevel.Debug, "Add NuGet package '{0}' to processing...", nugetId);
                    mNuGetPackages.Add(nugetId, nuspecPath);
                }
            }
        }
        /// <summary>
        /// Inspect each given nuget package for either license or licenseUrl tags. Download license if necessary.
        /// </summary>
        public void GetNuGetLicenseInfo()
        {
            if (mFinishProcessing)
            {
                // already finished processing
                return;
            }

            if (mNuGetPackages == null || mNuGetPackages.Count == 0)
            {
                // no NuGet packages found to process.
                sLog.Write(LogLevel.Notice, "There are no NuGet packages found.");
                return;
            }

            foreach (string nuSpecFilePath in mNuGetPackages.Values)
            {
                sLog.Write(LogLevel.Debug, "Begin retrieving NuGet specification information from '{0}'...", nuSpecFilePath);

                // extract important information from NuGet specification file
                var doc = new XmlDocument();
                doc.Load(nuSpecFilePath);
                XmlNode root = doc.DocumentElement;
                if (root == null)
                {
                    continue;
                }

                var namespaceManager = new XmlNamespaceManager(doc.NameTable);
                namespaceManager.AddNamespace("nu", root.NamespaceURI);

                string identifier =
                    root.SelectSingleNode("/nu:package/nu:metadata/nu:id", namespaceManager)?.InnerText ??
                    string.Empty;
                string version =
                    root.SelectSingleNode("/nu:package/nu:metadata/nu:version", namespaceManager)?.InnerText ??
                    string.Empty;
                string authors =
                    root.SelectSingleNode("/nu:package/nu:metadata/nu:authors", namespaceManager)?.InnerText ??
                    string.Empty;
                string licenseUrl =
                    root.SelectSingleNode("/nu:package/nu:metadata/nu:licenseUrl", namespaceManager)?.InnerText ??
                    string.Empty;
                if (licenseUrl == cDeprecatedLicenseUrl)
                {
                    licenseUrl = string.Empty;
                }
                string projectUrl =
                    root.SelectSingleNode("/nu:package/nu:metadata/nu:projectUrl", namespaceManager)?.InnerText ??
                    string.Empty;
                string copyright =
                    root.SelectSingleNode("/nu:package/nu:metadata/nu:copyright", namespaceManager)?.InnerText ??
                    string.Empty;
                XmlNode licenseNode = root.SelectSingleNode("/nu:package/nu:metadata/nu:license", namespaceManager);
                var     license     = string.Empty;
                if (licenseNode != null)
                {
                    switch (licenseNode.Attributes["type"].Value)
                    {
                    // TODO: Is there a better handling e.g. loading templates?
                    case "expression":
                        // SPDX expression as defined in https://spdx.org/spdx-specification-21-web-version#h.jxpfx0ykyb60
                        license = licenseNode.InnerText ?? string.Empty;
                        break;

                    case "file":
                        // license is contained as file within the nuget package
                        string directoryPath = Path.GetDirectoryName(nuSpecFilePath);
                        string licensePath   = Path.Combine(directoryPath, licenseNode.InnerText);
                        if (!File.Exists(licensePath))
                        {
                            // License file does not exist, try to open NuGet package on same folder hierarchy
                            // this could happen if a packages.config was processed and only the nuspec was unzipped.
                            string nugetPackage = Path.Combine(directoryPath, $"{identifier}.{version}.nupkg");
                            if (!File.Exists(nugetPackage))
                            {
                                sLog.Write(
                                    LogLevel.Error,
                                    "The path '{0}' to the license defined by the NuGet specification does not exists.",
                                    licensePath);
                                break;
                            }

                            // try to extract license file from package
                            using (var fs = new FileStream(nugetPackage, FileMode.Open))
                            {
                                var             archive      = new ZipArchive(fs, ZipArchiveMode.Read);
                                ZipArchiveEntry licenseEntry = archive.Entries.First(x => x.Name.Contains(licenseNode.InnerText));
                                if (licenseEntry == null)
                                {
                                    sLog.Write(LogLevel.Error, "The NuGet package '{0}' does not contains '{1}'", licenseNode.InnerText);
                                    break;
                                }

                                using (var licenseStream = new FileStream(licensePath, FileMode.Create, FileAccess.Write))
                                {
                                    licenseEntry.Open().CopyTo(licenseStream);
                                }
                            }
                        }

                        license = File.ReadAllText(licensePath) ?? string.Empty;
                        break;
                    }
                }

                // TODO: extend to download other url then from github.com
                if (string.IsNullOrEmpty(license) && !string.IsNullOrEmpty(licenseUrl) &&
                    (licenseUrl.Contains("github.com") || licenseUrl.Contains("/raw.githubusercontent.com/")))
                {
                    // download license when only url to license is given
                    string url = licenseUrl.Contains("github.com") ? licenseUrl.Replace("/blob/", "/raw/") : licenseUrl;
                    try
                    {
                        using (var client = new WebClient())
                        {
                            license = client.DownloadString(url);
                            sLog.Write(LogLevel.Debug, "Successful downloaded license '{0}' for {1}", url, identifier);
                        }
                    }
                    catch (WebException ex)
                    {
                        sLog.Write(LogLevel.Error, "Error during downloading '{0}': {1}", url, ex.Message);
                    }
                }

                if (string.IsNullOrEmpty(license) && string.IsNullOrEmpty(licenseUrl))
                {
                    sLog.Write(LogLevel.Error, "The NuGet specification file '{0}' does not contain valid license information", nuSpecFilePath);
                    continue;
                }

                var package = new PackageLicenseInfo(identifier, version, authors, copyright, licenseUrl, projectUrl, license);
                mLicenses.Add(package);
                sLog.Write(LogLevel.Debug, "Successful extract license information for '{0} v{1}'.", identifier, version);
            }

            sLog.Write(LogLevel.Notice, "Successful extract license information from found NuGet packages.");
            sLog.Write(LogLevel.Notice, "--------------------------------------------------------------------------------");
        }
Exemple #11
0
        private void ParseAuditFvdlWithXmlReader(ZipArchiveEntry zipArchiveEntry, string systemName)
        {
            try
            {
                XmlReaderSettings xmlReaderSettings = GenerateXmlReaderSettings();
                using (XmlReader xmlReader = XmlReader.Create(zipArchiveEntry.Open(), xmlReaderSettings))
                {
                    while (xmlReader.Read())
                    {
                        if (xmlReader.IsStartElement())
                        {
                            switch (xmlReader.Name)
                            {
                            case "CreatedTS":
                            {
                                lastObserved = DateTime.Parse(xmlReader.GetAttribute("date")).ToLongDateString();
                                break;
                            }

                            case "BuildID":
                            {
                                CreateAddAssetCommand(xmlReader, systemName);
                                break;
                            }

                            case "Vulnerability":
                            {
                                ParseFvdlVulnerablityNode(xmlReader);
                                break;
                            }

                            case "Description":
                            {
                                ParseFvdlDescriptionNode(xmlReader);
                                break;
                            }

                            case "EngineVersion":
                            {
                                CreateUpdateSourceCommand(xmlReader);
                                break;
                            }

                            default:
                            { break; }
                            }
                        }
                    }
                }
                foreach (FprVulnerability fprVulnerability in fprVulnerabilityList)
                {
                    using (SQLiteCommand sqliteCommand = FindingsDatabaseActions.sqliteConnection.CreateCommand())
                    { AddVulnerabilityAndUniqueFinding(fprVulnerability); }
                }
            }
            catch (Exception exception)
            {
                log.Error("Unable to read \"audit.fvdl\".");
                throw exception;
            }
        }
Exemple #12
0
 public ZipArchiveEntryWrap(ZipArchiveEntry instance)
 {
     this.Instance = instance;
 }
Exemple #13
0
 static void Main()
 {
     using ZipArchive zipFile = ZipFile.Open("zipFile.zip", ZipArchiveMode.Create);
     ZipArchiveEntry zipArchiveEntry = zipFile.CreateEntryFromFile("copyMe.png", "copyMeEntry.png");
 }
Exemple #14
0
        private void ButtomCompute_Click(object sender, RoutedEventArgs e)
        {
            ButtomCompute.IsEnabled = false;
            ButtonSelectDestinationDirectory.IsEnabled = false;
            ButtonSelectSourceDirectory.IsEnabled      = false;

            if (TextBoxSourceDirectory.Text == NODIRSTR || TextBoxDestinationDirectory.Text == NODIRSTR)
            {
                System.Windows.MessageBox.Show("Please select both a source and destination directory.");
                return;
            }

            DirectoryInfo SourceDirectory = new DirectoryInfo(TextBoxSourceDirectory.Text);

            if (!SourceDirectory.Exists)
            {
                System.Windows.MessageBox.Show(string.Format("Source directory \"{0}\" does not exist. Please use the \"Select...\" button to navigate to a real directory.", SourceDirectory.FullName)); return;
            }
            DirectoryInfo DestinationDirectory = new DirectoryInfo(TextBoxDestinationDirectory.Text);

            if (!DestinationDirectory.Exists)
            {
                System.Windows.MessageBox.Show(string.Format("Destination directory \"{0}\" does not exist. Please use the \"Select...\" button to navigate to a real directory.", SourceDirectory.FullName)); return;
            }

            ButtomCompute.Visibility = Visibility.Hidden;
            ButtonSelectDestinationDirectory.Visibility = Visibility.Hidden;
            ButtonSelectSourceDirectory.Visibility      = Visibility.Hidden;
            TextBoxDestinationDirectory.Visibility      = Visibility.Hidden;
            TextBoxSourceDirectory.Visibility           = Visibility.Hidden;
            ProgressBarZIPRip.Visibility = Visibility.Visible;
            LabelProgress.Visibility     = Visibility.Visible;

            FileInfo[]    ZIPFiles     = SourceDirectory.GetFiles("*_fastqc.zip");
            string[]      FQZDataSplit = null;
            string        rootname;
            List <string> Summary = new List <string>();
            int           nFiles  = ZIPFiles.Length;

            for (int i = 0; i < nFiles; i++)
            {
                LabelProgress.Content = string.Format("Processing file {0} of {1} ({2:0.0}%)...", i, nFiles, ((double)i / nFiles) * 100);
                FileInfo FastQCZipFile = ZIPFiles[i];
                Summary.Add("Module Name\tStatus\n");
                //Extract the stats from their containing file
                using (ZipArchive FQZip = ZipFile.OpenRead(FastQCZipFile.FullName)) {
                    rootname = FastQCZipFile.Name.Substring(0, FastQCZipFile.Name.Length - 4);
                    ZipArchiveEntry FQZData = FQZip.Entries.Where(n => n.Name == "fastqc_data.txt").First();                     //Filenames are without directory
                    FQZDataSplit = new StreamReader(FQZData.Open()).ReadToEnd().Split(new string[] { ">>" }, StringSplitOptions.RemoveEmptyEntries);
                }
                //Probably a bad idea but hey it's only ~270KB so lets do this in-memory
                //File contains _all_ records, encapsulated by >>MODULE NAME\t<pass|fail>\n[...]>>END MODULE, thus, split by '>>'
                FQZDataSplit = FQZDataSplit.Where(n => (n != "END_MODULE\n")).Skip(1).ToArray();
                //now each line represents one dataset, to be written to one file
                //(first entry is the fast qc version info, and thus skipped)
                foreach (string FQZDataFile in FQZDataSplit)
                {
                    string[] FQZSplit   = FQZDataFile.Split('\n');
                    string   moduleName = FQZSplit[0];                                 //first line contains the module name and pass/fail
                    Summary.Add(moduleName);
                    moduleName  = rootname + "_" + moduleName.Split('\t')[0] + ".txt"; //Create output file named based on the input file and current module
                    FQZSplit[1] = FQZSplit[1].Replace("#", string.Empty);              //correct headers
                    try {
                        FileInfo OutputFile = new FileInfo(System.IO.Path.Combine(DestinationDirectory.FullName, moduleName));
                        using (StreamWriter OutputFileWriter = new StreamWriter(OutputFile.Create())) { OutputFileWriter.Write(string.Join("\n", FQZSplit.Skip(1))); }                         //dump stats to file
                    }
                    catch (UnauthorizedAccessException) { System.Windows.MessageBox.Show("The program is unable to gain access to the target directory. Please select a new output directory or re-run the application with elevated privileges."); return; }
                    catch (PathTooLongException) { System.Windows.MessageBox.Show("The program cannot create the output file, as its full path would be longer than the system maximum. Please select a different output folder with a shorter path."); return; }
                }

                //repeat data dump for summary
                try {
                    FileInfo OutputFile = new FileInfo(System.IO.Path.Combine(DestinationDirectory.FullName, (rootname + "_Summary.txt")));
                    using (StreamWriter OutputFileWriter = new StreamWriter(OutputFile.Create())) { foreach (string s in Summary)
                                                                                                    {
                                                                                                        OutputFileWriter.Write(s + "\n");
                                                                                                    }
                    }
                }
                catch (UnauthorizedAccessException) { System.Windows.MessageBox.Show("The program is unable to gain access to the target directory. Please select a new output directory or re-run the application with elevated privileges."); }
                catch (PathTooLongException) { System.Windows.MessageBox.Show("The program cannot create the output file, as its full path would be longer than the system maximum. Please select a different output folder with a shorter path."); }
                //Update GUI - supposed to be a BackgroundWorkerThread...
            }

            ButtomCompute.Visibility = Visibility.Visible;
            ButtonSelectDestinationDirectory.Visibility = Visibility.Visible;
            ButtonSelectSourceDirectory.Visibility      = Visibility.Visible;
            TextBoxDestinationDirectory.Visibility      = Visibility.Visible;
            TextBoxSourceDirectory.Visibility           = Visibility.Visible;
            ProgressBarZIPRip.Visibility = Visibility.Hidden;
            LabelProgress.Visibility     = Visibility.Hidden;
            ButtomCompute.IsEnabled      = true;
            ButtonSelectDestinationDirectory.IsEnabled = true;
            ButtonSelectSourceDirectory.IsEnabled      = true;
            System.Windows.MessageBox.Show("Operation complete. Extracted " + nFiles + " FastQC files into individual .txt files.\nHave a nice day!");
            //Done!
        }
 public ZippedFileInfo(ZipArchiveEntry entry)
 {
     _entry = entry;
 }
Exemple #16
0
 private string ComputeEntryHash(ZipArchiveEntry entry, S3WriterResourceProperties properties)
 => $"{GetMD5AsHexString(entry)}-{DetermineEncodingType(entry.FullName, properties)}";
        public static bool UnzipMod(this InstallerWindow ins, Stream zs)
        {
            string platform = "";

            if (ETGFinder.Platform.HasFlag(ETGPlatform.Windows))
            {
                platform = "win32";
            }
            else if (ETGFinder.Platform.HasFlag(ETGPlatform.MacOS))
            {
                platform = "osx";
            }
            else if (ETGFinder.Platform.HasFlag(ETGPlatform.Linux))
            {
                platform = ETGFinder.Platform.HasFlag(ETGPlatform.X64) ? "lib64" : "lib";
            }

            string fallback = "ETGMOD";
            string prefix   = fallback;

            if (ETGFinder.Version.Contains("b"))
            {
                prefix += "-BETA";
            }

            fallback += "/";
            prefix   += "/";

            string pathGame = ins.MainModDir;

            ins.Log("Checking for ").Log(prefix).LogLine("...");

            using (ZipArchive zip = new ZipArchive(zs, ZipArchiveMode.Read)) {
                int prefixCount   = 0;
                int fallbackCount = 0;
                int noneCount     = 0;
                ins.InitProgress("Scanning ZIP", zip.Entries.Count);
                for (int i = 0; i < zip.Entries.Count; i++)
                {
                    ins.SetProgress(i);
                    ZipArchiveEntry entry = zip.Entries[i];
                    ins.Log("Entry: ").Log(entry.FullName).Log(": ").Log(entry.Length.ToString()).LogLine(" bytes");

                    if (entry.FullName == "InstallerVersion.txt")
                    {
                        ins.LogLine("Found version file.");

                        using (Stream s = entry.Open()) {
                            using (StreamReader sr = new StreamReader(s)) {
                                Version minv = new Version(sr.ReadLine().Trim());
                                if (InstallerWindow.Version < minv)
                                {
                                    ins.LogLine("There's a new ETGMod Installer version!");
                                    ins.LogLine("Visit https://modthegungeon.github.io/#download to download it.");
                                    ins.Log("(Minimum installer version for this ETGMod version: ").LogLine(minv.ToString()).Log(")");
                                    ins.Invoke(() => ins.Progress.BrushProgress =
                                                   new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 63, 63, 91))
                                               );
                                    ins.InitProgress("Installer update required!", 1).SetProgress(1);
                                    return(false);
                                }
                            }
                        }

                        continue;
                    }

                    string entryName = entry.FullName;
                    if (entry.FullName.StartsWith(prefix))
                    {
                        prefixCount++;
                    }
                    else if (entry.FullName.StartsWith(fallback))
                    {
                        fallbackCount++;
                    }
                    else
                    {
                        noneCount++;
                    }
                }

                if (0 < prefixCount)
                {
                    ins.Log(prefix).LogLine(" found.");
                    ins.InitProgress("Extracting ZIP", prefixCount);
                }
                else if (0 < fallbackCount)
                {
                    ins.Log("Didn't find ").Log(prefix).Log(", falling back to ").Log(fallback).LogLine(".");
                    prefix = fallback;
                    ins.InitProgress("Extracting ZIP", fallbackCount);
                }
                else
                {
                    ins.LogLine("Is this even a ETGMod ZIP? uh...");
                    prefix = "";
                    ins.InitProgress("Extracting ZIP", noneCount);
                }

                int extracted = 0;
                for (int i = 0; i < zip.Entries.Count; i++)
                {
                    ZipArchiveEntry entry = zip.Entries[i];
                    if (!entry.FullName.StartsWith(prefix) || entry.FullName == prefix)
                    {
                        continue;
                    }
                    ins.SetProgress(++extracted);

                    string entryName = entry.FullName.Substring(prefix.Length);

                    if (entryName.StartsWith("LIBS/"))
                    {
                        entryName = entryName.Substring(5);
                        if (!entryName.StartsWith(platform + "/"))
                        {
                            continue;
                        }
                        entryName = entryName.Substring(platform.Length + 1);
                    }

                    entryName = entryName.Replace('/', Path.DirectorySeparatorChar);

                    string path = Path.Combine(pathGame, entryName);
                    ins.Log("Extracting: ").Log(entry.FullName).Log(" -> ").LogLine(path);
                    if (entry.Length == 0 && entry.CompressedLength == 0)
                    {
                        Directory.CreateDirectory(path);
                    }
                    else
                    {
                        entry.ExtractToFile(path, true);
                    }
                }
                ins.EndProgress("Extracted ZIP.");
            }

            return(true);
        }
Exemple #18
0
        public static async Task ReadInterleaved()
        {
            using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"))))
            {
                ZipArchiveEntry e1 = archive.GetEntry("first.txt");
                ZipArchiveEntry e2 = archive.GetEntry("notempty/second.txt");

                //read all of e1 and e2's contents
                Byte[] e1readnormal  = new Byte[e1.Length];
                Byte[] e2readnormal  = new Byte[e2.Length];
                Byte[] e1interleaved = new Byte[e1.Length];
                Byte[] e2interleaved = new Byte[e2.Length];

                using (Stream e1s = e1.Open())
                {
                    ZipTest.ReadBytes(e1s, e1readnormal, e1.Length);
                }
                using (Stream e2s = e2.Open())
                {
                    ZipTest.ReadBytes(e2s, e2readnormal, e2.Length);
                }

                //now read interleaved, assume we are working with < 4gb files
                const Int32 bytesAtATime = 15;

                using (Stream e1s = e1.Open(), e2s = e2.Open())
                {
                    Int32 e1pos = 0;
                    Int32 e2pos = 0;

                    while (e1pos < e1.Length || e2pos < e2.Length)
                    {
                        if (e1pos < e1.Length)
                        {
                            Int32 e1bytesRead = e1s.Read(e1interleaved, e1pos,
                                                         bytesAtATime + e1pos > e1.Length ? (Int32)e1.Length - e1pos : bytesAtATime);
                            e1pos += e1bytesRead;
                        }

                        if (e2pos < e2.Length)
                        {
                            Int32 e2bytesRead = e2s.Read(e2interleaved, e2pos,
                                                         bytesAtATime + e2pos > e2.Length ? (Int32)e2.Length - e2pos : bytesAtATime);
                            e2pos += e2bytesRead;
                        }
                    }
                }

                //now compare to original read
                ZipTest.ArraysEqual <Byte>(e1readnormal, e1interleaved, e1readnormal.Length);
                ZipTest.ArraysEqual <Byte>(e2readnormal, e2interleaved, e2readnormal.Length);

                //now read one entry interleaved
                Byte[] e1selfInterleaved1 = new Byte[e1.Length];
                Byte[] e1selfInterleaved2 = new Byte[e2.Length];


                using (Stream s1 = e1.Open(), s2 = e1.Open())
                {
                    Int32 s1pos = 0;
                    Int32 s2pos = 0;

                    while (s1pos < e1.Length || s2pos < e1.Length)
                    {
                        if (s1pos < e1.Length)
                        {
                            Int32 s1bytesRead = s1.Read(e1interleaved, s1pos,
                                                        bytesAtATime + s1pos > e1.Length ? (Int32)e1.Length - s1pos : bytesAtATime);
                            s1pos += s1bytesRead;
                        }

                        if (s2pos < e1.Length)
                        {
                            Int32 s2bytesRead = s2.Read(e2interleaved, s2pos,
                                                        bytesAtATime + s2pos > e1.Length ? (Int32)e1.Length - s2pos : bytesAtATime);
                            s2pos += s2bytesRead;
                        }
                    }
                }

                //now compare to original read
                ZipTest.ArraysEqual <Byte>(e1readnormal, e1selfInterleaved1, e1readnormal.Length);
                ZipTest.ArraysEqual <Byte>(e1readnormal, e1selfInterleaved2, e1readnormal.Length);
            }
        }
Exemple #19
0
        private void reloadUI(string root)
        {
            currentRoot = root;

            mods.Clear();
            modZips.Clear();
            lstRaces.Items.Clear();
            lstMods.Items.Clear();
            llToxicRagers.Text = tagLines[new Random().Next(tagLines.Count)];
            txtPath.Text       = root;
            btnInstall.Text    = "select a mod and a map to replace, then click here!";
            btnInstall.Enabled = false;

            string twtFile = Path.Combine(txtPath.Text, "data.twt");

            if (File.Exists(twtFile))
            {
                TWT twt = TWT.Load(twtFile);

                foreach (TWTEntry entry in twt.Contents)
                {
                    twt.Extract(entry, Path.Combine(txtPath.Text, "data"));
                }

                File.Move(twtFile, Path.Combine(txtPath.Text, "data.twat"));
            }

            string racesTXT = Path.Combine(txtPath.Text, "data", "races.txt");

            if (File.Exists(racesTXT))
            {
                races = RacesTXT.Load(Path.Combine(txtPath.Text, "data", "races.txt"));

                foreach (RaceDetails race in races.Races)
                {
                    if (!race.BoundaryRace)
                    {
                        lstRaces.Items.Add(race.Name);
                    }
                }
            }

            string mapModDir = Path.Combine(txtPath.Text, ".mods", "maps");

            if (Directory.Exists(mapModDir))
            {
                foreach (string file in Directory.GetFiles(mapModDir, "*.zip"))
                {
                    using (FileStream fs = new FileStream(file, FileMode.Open))
                        using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Read))
                        {
                            if (archive.Entries.Any(f => f.Name.EndsWith(".c2t", StringComparison.InvariantCultureIgnoreCase)) &&
                                archive.Entries.Any(f => f.Name == "" && f.FullName.ToLower() == @"data/races/"))
                            {
                                ZipArchiveEntry entry = archive.Entries.First(f => f.Name.EndsWith(".c2t", StringComparison.InvariantCultureIgnoreCase));

                                using (MemoryStream ms = new MemoryStream())
                                    using (BinaryReader br = new BinaryReader(ms))
                                    {
                                        entry.Open().CopyTo(ms);

                                        ms.Seek(0, SeekOrigin.Begin);

                                        RaceDetails details = RaceDetails.Load(new DocumentParser(br));

                                        int index = lstMods.Items.Add(details.Name);

                                        modZips.Add(index, file);
                                        mods.Add(details);
                                    }
                            }
                        }
                }
            }

            foreach (string file in Directory.GetFiles(txtPath.Text, "*.c2t"))
            {
                RaceDetails details = RaceDetails.Load(new DocumentParser(file));

                if (!mods.Any(m => m.Name == details.Name))
                {
                    lstMods.Items.Add(details.Name);
                    mods.Add(details);
                }
            }
        }
Exemple #20
0
        public static void IsZipSameAsDir(Stream archiveFile, String directory, ZipArchiveMode mode, Boolean dontRequireExplicit, Boolean dontCheckTimes)
        {
            int count = 0;

            using (ZipArchive archive = new ZipArchive(archiveFile, mode))
            {
                var allFilesInDir = FileData.InPath(directory);
                foreach (var file in allFilesInDir)
                {
                    count++;
                    String entryName = file.FullName;
                    if (file.IsFolder)
                    {
                        entryName += Path.DirectorySeparatorChar;
                    }

                    ZipArchiveEntry entry = archive.GetEntry(entryName);
                    if (entry == null)
                    {
                        entryName = FlipSlashes(entryName);
                        entry     = archive.GetEntry(entryName);
                    }

                    if (file.IsFile)
                    {
                        try
                        {
                            Assert.NotNull(entry);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("File Entry {0} in directory but not archive: {1}", entryName, file.FullName);
                            throw;
                        }

                        Int64 givenLength = entry.Length;

                        var buffer = new byte[entry.Length];
                        using (Stream entrystream = entry.Open())
                        {
                            entrystream.Read(buffer, 0, buffer.Length);
                            String crc = CRC.CalculateCRC(buffer);
                            Assert.Equal(file.Length, givenLength);
                            Assert.Equal(file.CRC, crc);
                        }

                        if (!dontCheckTimes)
                        {
                            Double offBy = (file.LastModifiedDate - entry.LastWriteTime.DateTime).TotalSeconds;
                            Assert.True(
                                (offBy >= -2 && offBy <= 2) ||
                                // Temporary adjustment for active issue 1326
                                ((offBy >= 3598 && offBy <= 3602)),
                                String.Format("{0}, {1}, {2}", file.LastModifiedDate.ToString(), entry.LastWriteTime.DateTime.ToString(), file.FullName));
                        }

                        Assert.Equal(file.Name, entry.Name);
                        Assert.Equal(entryName, entry.FullName);
                        Assert.Equal(entryName, entry.ToString());
                        Assert.Equal(archive, entry.Archive);
                    }
                    else if (file.IsFolder)
                    {
                        if (entry == null) //entry not found
                        {
                            string  entryNameOtherSlash = FlipSlashes(entryName);
                            Boolean isEmtpy             = !allFilesInDir.Any(
                                f => f.IsFile &&
                                (f.FullName.StartsWith(entryName, StringComparison.OrdinalIgnoreCase) ||
                                 f.FullName.StartsWith(entryNameOtherSlash, StringComparison.OrdinalIgnoreCase)));
                            if ((!dontRequireExplicit || isEmtpy) && !entryName.Contains("emptydir"))
                            {
                                Assert.True(false, String.Format("Folder Entry {0} in directory but not archive: {1}", entryName, directory));
                            }

                            if ((dontRequireExplicit && !isEmtpy) || entryName.Contains("emptydir"))
                            {
                                count--; //discount this entry
                            }
                        }
                        else
                        {
                            using (Stream es = entry.Open())
                            {
                                try
                                {
                                    Assert.True(es.Length == 0, "Length should be 0");
                                }
                                catch (NotSupportedException)
                                {
                                    try
                                    {
                                        Assert.Equal(-1, es.ReadByte());
                                    }
                                    catch (Exception)
                                    {
                                        Console.WriteLine("Didn't return EOF");
                                        throw;
                                    }
                                }
                            }
                        }
                    }
                }

                Assert.Equal(count, archive.Entries.Count);
            }
        }
Exemple #21
0
        private async Task <Etag> ExportFiles(ZipArchive archive, Etag lastEtag, Etag maxEtag)
        {
            var totalCount     = 0;
            var lastReport     = SystemTime.UtcNow;
            var reportInterval = TimeSpan.FromSeconds(2);

            Operations.ShowProgress("Exporting Files");

            var metadataList = new List <FileContainer>();

            Exception exceptionHappened = null;

            try
            {
                while (true)
                {
                    bool hasDocs = false;
                    using (var files = await Operations.GetFiles(lastEtag, Options.BatchSize))
                    {
                        while (await files.MoveNextAsync())
                        {
                            hasDocs = true;
                            var file = files.Current;
                            if (file.IsTombstone)
                            {
                                lastEtag = file.Etag;
                                continue;
                            }

                            var tempLastEtag = file.Etag;
                            if (maxEtag != null && tempLastEtag.CompareTo(maxEtag) > 0)
                            {
                                break;
                            }

                            // Write the metadata (which includes the stream size and file container name)
                            var fileContainer = new FileContainer
                            {
                                Key      = Path.Combine(file.Directory.TrimStart('/'), file.Name),
                                Metadata = file.Metadata,
                            };

                            ZipArchiveEntry fileToStore = archive.CreateEntry(fileContainer.Key);

                            using (var fileStream = await Operations.DownloadFile(file))
                                using (var zipStream = fileToStore.Open())
                                {
                                    await fileStream.CopyToAsync(zipStream).ConfigureAwait(false);
                                }

                            metadataList.Add(fileContainer);

                            lastEtag = tempLastEtag;

                            totalCount++;
                            if (totalCount % 1000 == 0 || SystemTime.UtcNow - lastReport > reportInterval)
                            {
                                //TODO: Show also the MB/sec and total GB exported.
                                Operations.ShowProgress("Exported {0} files. ", totalCount);
                                lastReport = SystemTime.UtcNow;
                            }
                        }
                    }
                    if (!hasDocs)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Operations.ShowProgress("Got Exception during smuggler export. Exception: {0}. ", e.Message);
                Operations.ShowProgress("Done with reading files, total: {0}, lastEtag: {1}", totalCount, lastEtag);

                exceptionHappened = new SmugglerExportException(e.Message, e)
                {
                    LastEtag = lastEtag,
                };
            }

            var metadataEntry = archive.CreateEntry(MetadataEntry);

            using (var metadataStream = metadataEntry.Open())
                using (var writer = new StreamWriter(metadataStream))
                {
                    foreach (var item in metadataList)
                    {
                        writer.WriteLine(RavenJObject.FromObject(item));
                    }
                }

            if (exceptionHappened != null)
            {
                throw exceptionHappened;
            }

            Operations.ShowProgress("Done with reading documents, total: {0}, lastEtag: {1}", totalCount, lastEtag);
            return(lastEtag);
        }
Exemple #22
0
        /// <summary>
        /// Processes input queue
        /// </summary>
        /// <returns>
        /// Task object for continuation
        /// </returns>
        private async Task ProcessInputQueue()
        {
            using (_httpClientHelper = new HttpClientHelper(_settings))
            {
                var        fileCount = 0;
                string     fileNameInPackageTemplate = "";
                FileStream zipToOpen = null;
                ZipArchive archive   = null;

                if (_settings.InputFilesArePackages == false)
                {
                    fileNameInPackageTemplate = GetFileNameInPackageTemplate();
                    if (string.IsNullOrEmpty(fileNameInPackageTemplate))
                    {
                        throw new JobExecutionException(string.Format(Resources.Job_0_Please_check_your_package_template_Input_file_name_in_Manifest_cannot_be_identified, _context.JobDetail.Key));
                    }
                }

                while (InputQueue.TryDequeue(out DataMessage dataMessage))
                {
                    try
                    {
                        if (fileCount > 0 && _settings.DelayBetweenFiles > 0) //Only delay after first file and never after last.
                        {
                            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(_settings.DelayBetweenFiles));
                        }
                        fileCount++;

                        var sourceStream = _retryPolicyForIo.Execute(() => FileOperationsHelper.Read(dataMessage.FullPath));
                        if (sourceStream == null)
                        {
                            continue;                      //Nothing to do here
                        }
                        string tempFileName = "";

                        //If we need to "wrap" file in package envelope
                        if (_settings.InputFilesArePackages == false)
                        {
                            using (zipToOpen = new FileStream(_settings.PackageTemplate, FileMode.Open))
                            {
                                tempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                                _retryPolicyForIo.Execute(() => FileOperationsHelper.Create(zipToOpen, tempFileName));
                                var tempZipStream = _retryPolicyForIo.Execute(() => FileOperationsHelper.Read(tempFileName));
                                using (archive = new ZipArchive(tempZipStream, ZipArchiveMode.Update))
                                {
                                    //Check if package template contains input file and remove it first. It should not be there in the first place.
                                    ZipArchiveEntry entry = archive.GetEntry(fileNameInPackageTemplate);
                                    if (entry != null)
                                    {
                                        entry.Delete();
                                        Log.WarnFormat(CultureInfo.InvariantCulture, string.Format(Resources.Job_0_Package_template_contains_input_file_1_Please_remove_it_from_the_template, _context.JobDetail.Key, fileNameInPackageTemplate));
                                    }

                                    // Update Manifest file with the original file name for end-to-end traceability. Use the new file name in the rest of the method.
                                    fileNameInPackageTemplate = UpdateManifestFile(archive, dataMessage, fileNameInPackageTemplate);

                                    var importedFile = archive.CreateEntry(fileNameInPackageTemplate, CompressionLevel.Fastest);
                                    using var entryStream = importedFile.Open();
                                    sourceStream.CopyTo(entryStream);
                                    sourceStream.Close();
                                    sourceStream.Dispose();
                                }
                                sourceStream = _retryPolicyForIo.Execute(() => FileOperationsHelper.Read(tempFileName));
                            }
                        }
                        if (Log.IsDebugEnabled)
                        {
                            Log.DebugFormat(CultureInfo.InvariantCulture, string.Format(Resources.Job_0_Uploading_file_1_File_size_2_bytes, _context.JobDetail.Key, dataMessage.FullPath.Replace(@"{", @"{{").Replace(@"}", @"}}"), sourceStream.Length));
                        }

                        // Get blob url and id. Returns in json format
                        var response = await _httpClientHelper.GetAzureWriteUrl();

                        if (!response.IsSuccessStatusCode)
                        {
                            throw new JobExecutionException($"Job: {_settings.JobKey}. Request GetAzureWriteUrl failed.");
                        }
                        var blobInfo = (JObject)JsonConvert.DeserializeObject(HttpClientHelper.ReadResponseString(response));
                        var blobUrl  = blobInfo["BlobUrl"].ToString();

                        var blobUri = new Uri(blobUrl);

                        //Upload package to blob storage
                        var uploadResponse = await _httpClientHelper.UploadContentsToBlob(blobUri, sourceStream);

                        if (sourceStream != null)
                        {
                            sourceStream.Close();
                            sourceStream.Dispose();
                            if (!_settings.InputFilesArePackages)//if we wraped file in package envelop we need to delete temp file
                            {
                                _retryPolicyForIo.Execute(() => FileOperationsHelper.Delete(tempFileName));
                            }
                        }
                        if (uploadResponse.IsSuccessStatusCode)
                        {
                            //Now send import request
                            var targetLegalEntity = _settings.Company;
                            if (_settings.MultiCompanyImport && _settings.GetLegalEntityFromSubfolder)
                            {
                                targetLegalEntity = new FileInfo(dataMessage.FullPath).Directory.Name;
                            }
                            if (_settings.MultiCompanyImport && _settings.GetLegalEntityFromFilename)
                            {
                                String[] separator = { _settings.FilenameSeparator };
                                var      tokenList = dataMessage.Name.Split(separator, 10, StringSplitOptions.RemoveEmptyEntries);

                                targetLegalEntity = tokenList[_settings.LegalEntityTokenPosition - 1];
                            }
                            if (targetLegalEntity.Length > 4)
                            {
                                Log.ErrorFormat(CultureInfo.InvariantCulture, string.Format(Resources.Job_0_Target_legal_entity_is_not_valid_1, _context.JobDetail.Key, targetLegalEntity));
                            }

                            if (string.IsNullOrEmpty(targetLegalEntity))
                            {
                                throw new Exception(string.Format(Resources.Job_0_Unable_to_get_target_legal_entity_name, _context.JobDetail.Key));
                            }
                            var executionIdGenerated = CreateExecutionId(_settings.DataProject);
                            var importResponse       = await _httpClientHelper.ImportFromPackage(blobUri.AbsoluteUri, _settings.DataProject, executionIdGenerated, _settings.ExecuteImport, _settings.OverwriteDataProject, targetLegalEntity);

                            if (importResponse.IsSuccessStatusCode)
                            {
                                var    result       = importResponse.Content.ReadAsStringAsync().Result;
                                var    jsonResponse = (JObject)JsonConvert.DeserializeObject(result);
                                string executionId  = jsonResponse["value"].ToString();

                                var targetDataMessage = new DataMessage(dataMessage)
                                {
                                    MessageId     = executionId,
                                    FullPath      = dataMessage.FullPath.Replace(_settings.InputDir, _settings.UploadSuccessDir),
                                    MessageStatus = MessageStatus.Enqueued
                                };

                                // Move to inprocess/success location
                                _retryPolicyForIo.Execute(() => FileOperationsHelper.Move(dataMessage.FullPath, targetDataMessage.FullPath));

                                if (_settings.ExecutionJobPresent)
                                {
                                    _retryPolicyForIo.Execute(() => FileOperationsHelper.WriteStatusFile(targetDataMessage, _settings.StatusFileExtension));
                                }
                                if (Log.IsDebugEnabled)
                                {
                                    Log.DebugFormat(CultureInfo.InvariantCulture, string.Format(Resources.Job_0_File_1_uploaded_successfully, _context.JobDetail.Key, dataMessage.FullPath.Replace(@"{", @"{{").Replace(@"}", @"}}")));
                                }
                            }
                            else
                            {
                                // import request failed. Move message to error location.
                                Log.ErrorFormat(CultureInfo.InvariantCulture, string.Format(Resources.Job_0_Upload_failed_for_file_1_Failure_response_Status_2_Reason_3, _context.JobDetail.Key, dataMessage.FullPath.Replace(@"{", @"{{").Replace(@"}", @"}}"), importResponse.StatusCode, importResponse.ReasonPhrase, $"{Environment.NewLine}packageUrl: {blobUri.AbsoluteUri}{Environment.NewLine}definitionGroupId: {_settings.DataProject}{Environment.NewLine}executionId: {executionIdGenerated}{Environment.NewLine}execute: {_settings.ExecuteImport}{Environment.NewLine}overwrite: {_settings.OverwriteDataProject}{Environment.NewLine}legalEntityId: {targetLegalEntity}"));

                                var targetDataMessage = new DataMessage(dataMessage)
                                {
                                    FullPath      = dataMessage.FullPath.Replace(_settings.InputDir, _settings.UploadErrorsDir),
                                    MessageStatus = MessageStatus.Failed
                                };

                                // Move data to error location
                                _retryPolicyForIo.Execute(() => FileOperationsHelper.Move(dataMessage.FullPath, targetDataMessage.FullPath));

                                // Save the log with import failure details
                                _retryPolicyForIo.Execute(() => FileOperationsHelper.WriteStatusLogFile(targetDataMessage, importResponse, _settings.StatusFileExtension));
                            }
                        }
                        else
                        {
                            // upload failed. Move message to error location.
                            Log.ErrorFormat(CultureInfo.InvariantCulture, string.Format(Resources.Job_0_Upload_failed_for_file_1_Failure_response_Status_2_Reason_3, _context.JobDetail.Key, dataMessage.FullPath.Replace(@"{", @"{{").Replace(@"}", @"}}"), uploadResponse.StatusCode, uploadResponse.ReasonPhrase));

                            var targetDataMessage = new DataMessage(dataMessage)
                            {
                                FullPath      = dataMessage.FullPath.Replace(_settings.InputDir, _settings.UploadErrorsDir),
                                MessageStatus = MessageStatus.Failed
                            };

                            // Move data to error location
                            _retryPolicyForIo.Execute(() => FileOperationsHelper.Move(dataMessage.FullPath, targetDataMessage.FullPath));

                            // Save the log with import failure details
                            _retryPolicyForIo.Execute(() => FileOperationsHelper.WriteStatusLogFile(targetDataMessage, uploadResponse, _settings.StatusFileExtension));
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.ErrorFormat(CultureInfo.InvariantCulture, string.Format(Resources.Job_0_Failure_processing_file_1_Exception_2, _context.JobDetail.Key, dataMessage.FullPath.Replace(@"{", @"{{").Replace(@"}", @"}}"), ex.Message), ex);
                        throw;
                    }
                    finally
                    {
                        if (zipToOpen != null)
                        {
                            zipToOpen.Close();
                            zipToOpen.Dispose();
                        }
                        archive?.Dispose();
                    }
                }
            }
        }
Exemple #23
0
 private static XDocument XDocumentFromZipArchiveEntry(ZipArchiveEntry entry)
 {
     using (var reportStream = entry.Open())
         return(XDocument.Load(reportStream));
 }
Exemple #24
0
        public string DestinationFile(ZipArchiveEntry entry)
        {
            string dest_file = Path.ChangeExtension(entry.Name, ".txt");

            return(dest_file);
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //Считывает полный путь к файлу
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter      = "Text documents (*.txt)|*.txt|All files (*.*)|*.*";
            dialog.FilterIndex = 2;

            Nullable <bool> result = dialog.ShowDialog();

            if (result == true)
            {
                // Open document
                filename = dialog.FileName;
                //TextBlock textBlock = new TextBlock();
                filePath       = System.IO.Path.GetDirectoryName(filename);
                textBlock.Text = filename;
            }

            string infoFile = "";

            for (int i = filePath.Length + 1; i < filename.Length; i++)
            {
                infoFile += filename[i];
            }

            //string sz = System.IO.File.ReadAllText(filePath + "\\" + "size.txt");

            /*var indxx = sz.IndexOf('@');
             * var indxx2 = sz.IndexOf('?');
             * string btSz = "";
             * string nbOfByte = "";
             * string extension = "";
             * for (int i = 0; i < indxx; i++)
             * {
             *  btSz += sz[i];
             * }
             *
             * for (int i = indxx + 1; i < indxx2; i++)
             * {
             *  nbOfByte += sz[i];
             * }
             *
             *
             * for (int i = indxx2 + 1; i < sz.Length; i++)
             * {
             *  extension += sz[i];
             * }*/



            string btSz = File.ReadLines(filePath + "\\" + infoFile).Skip(0).First();

            string nbOfByte = File.ReadLines(filePath + "\\" + infoFile).Skip(1).First();

            string extension = File.ReadLines(filePath + "\\" + infoFile).Skip(2).First();

            string flname = File.ReadLines(filePath + "\\" + infoFile).Skip(3).First();


            string[] allFoundFiles = Directory.GetFiles(filePath, "*@*.zip", SearchOption.AllDirectories);
            long     count         = 0;
            int      size          = Convert.ToInt32(btSz);
            int      bytesCount    = Convert.ToInt32(nbOfByte);

            byte[] byteArray = new byte[size];

            int[,] array = new int[allFoundFiles.Length, 2];
            string tm = "";

            for (int i = 0; i < allFoundFiles.Length; i++)
            {
                var a    = allFoundFiles[i];
                var fInd = a.IndexOf('@');
                tm = "";
                for (int j = fInd + 1; j < a.Length - extension.Length; j++)
                {
                    tm += a[j];
                }
                array[i, 0] = Convert.ToInt32(tm);
                array[i, 1] = i;
            }

            for (int i = 0; i < allFoundFiles.Length; i++)
            {
                for (int j = 0; j < allFoundFiles.Length - 1; j++)
                {
                    if (array[j, 0] > array[j + 1, 0])
                    {
                        int z = array[j, 0];
                        int y = array[j, 1];
                        array[j, 0]     = array[j + 1, 0];
                        array[j, 1]     = array[j + 1, 1];
                        array[j + 1, 0] = z;
                        array[j + 1, 1] = y;
                    }
                }
            }

            int[] indexOfFiles = new int[allFoundFiles.Length];

            for (int i = 0; i < allFoundFiles.Length; i++)
            {
                indexOfFiles[i] = array[i, 1];
            }


            // int iteratCount = 0;

            for (int i = 0; i < allFoundFiles.Length; i++)
            {
                using (FileStream zipToOpen = new FileStream(allFoundFiles[indexOfFiles[i]], FileMode.Open))
                {
                    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                    {
                        ZipArchiveEntry readmeEntry = archive.GetEntry(i + extension);
                        using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                        {
                            var _byte_counter = bytesCount;
                            while (_byte_counter > 0 && count < size)
                            {
                                _byte_counter--;
                                byteArray[count] = (byte)writer.BaseStream.ReadByte();
                                count++;
                            }
                        }
                    }
                }
            }

            using (FileStream zipToOpen = new FileStream(filePath + "\\" + flname + ".zip", FileMode.OpenOrCreate))
            {
                using (ZipArchive zip = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    ZipArchiveEntry readmeEntry = zip.CreateEntry(flname + extension);
                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                    {
                        foreach (var item in byteArray)
                        {
                            writer.BaseStream.WriteByte(item);
                            progressBar.Value++;
                        }
                    }
                }
            }
        }
        public ZipArchiveProblems Validate(ZipArchiveEntry entry, IDbaseRecordEnumerator <RoadSegmentWidthChangeDbaseRecord> records)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }
            if (records == null)
            {
                throw new ArgumentNullException(nameof(records));
            }

            var problems = ZipArchiveProblems.None;

            try
            {
                var identifiers = new Dictionary <AttributeId, RecordNumber>();
                var moved       = records.MoveNext();
                if (moved)
                {
                    while (moved)
                    {
                        var recordContext = entry.AtDbaseRecord(records.CurrentRecordNumber);
                        var record        = records.Current;
                        if (record != null)
                        {
                            if (!record.RECORDTYPE.HasValue)
                            {
                                problems += recordContext.RequiredFieldIsNull(record.RECORDTYPE.Field);
                            }
                            else
                            {
                                if (!RecordType.ByIdentifier.ContainsKey(record.RECORDTYPE.Value))
                                {
                                    problems += recordContext.RecordTypeMismatch(record.RECORDTYPE.Value);
                                }
                            }
                            if (record.WB_OIDN.HasValue)
                            {
                                if (record.WB_OIDN.Value == 0)
                                {
                                    problems += recordContext.IdentifierZero();
                                }
                                else
                                {
                                    var identifier = new AttributeId(record.WB_OIDN.Value);
                                    if (identifiers.TryGetValue(identifier, out var takenByRecordNumber))
                                    {
                                        problems += recordContext.IdentifierNotUnique(
                                            identifier,
                                            takenByRecordNumber
                                            );
                                    }
                                    else
                                    {
                                        identifiers.Add(identifier, records.CurrentRecordNumber);
                                    }
                                }
                            }
                            else
                            {
                                problems += recordContext.RequiredFieldIsNull(record.WB_OIDN.Field);
                            }

                            if (!record.BREEDTE.HasValue)
                            {
                                problems += recordContext.RequiredFieldIsNull(record.BREEDTE.Field);
                            }
                            else if (!RoadSegmentWidth.Accepts(record.BREEDTE.Value))
                            {
                                problems += recordContext.WidthOutOfRange(record.BREEDTE.Value);
                            }

                            if (!record.VANPOSITIE.HasValue)
                            {
                                problems += recordContext.RequiredFieldIsNull(record.VANPOSITIE.Field);
                            }
                            else if (!RoadSegmentPosition.Accepts(record.VANPOSITIE.Value))
                            {
                                problems += recordContext.FromPositionOutOfRange(record.VANPOSITIE.Value);
                            }

                            if (!record.TOTPOSITIE.HasValue)
                            {
                                problems += recordContext.RequiredFieldIsNull(record.TOTPOSITIE.Field);
                            }
                            else if (!RoadSegmentPosition.Accepts(record.TOTPOSITIE.Value))
                            {
                                problems += recordContext.ToPositionOutOfRange(record.TOTPOSITIE.Value);
                            }

                            if (!record.WS_OIDN.HasValue)
                            {
                                problems += recordContext.RequiredFieldIsNull(record.WS_OIDN.Field);
                            }
                            else if (!RoadSegmentId.Accepts(record.WS_OIDN.Value))
                            {
                                problems += recordContext.RoadSegmentIdOutOfRange(record.WS_OIDN.Value);
                            }
                        }
                        moved = records.MoveNext();
                    }
                }
                else
                {
                    problems += entry.HasNoDbaseRecords(false);
                }
            }
            catch (Exception exception)
            {
                problems += entry.AtDbaseRecord(records.CurrentRecordNumber).HasDbaseRecordFormatError(exception);
            }

            return(problems);
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (fileIsChecked)
            {
                progressBar.Value = 0;
                int SIZE = Convert.ToInt32(textBlock2.Text);

                filename = textBlock.Text;

                // Определяет единицу информации
                // По-умолчанию - 1Мб
                if (textBlock2.Text == null)
                {
                    SIZE = 1024;
                }
                if (cbx.SelectedItem.ToString() == "байт")
                {
                    SIZE *= 1;
                }
                else if (cbx.SelectedItem.ToString() == "кБ")
                {
                    SIZE *= 1024;
                }
                else if (cbx.SelectedItem.ToString() == "мБ")
                {
                    SIZE *= 1048576;
                }

                MAX = SIZE;

                byte[] bt = File.ReadAllBytes(filename);


                // Нахождения индекса элемента с которого начинается расширение
                int indxx = 0;
                for (int i = 0; i < filename.Length; i++)
                {
                    if (filename[i] == '.')
                    {
                        indxx = i;
                    }
                }

                string extension = "";
                for (int i = indxx; i < filename.Length; i++)
                {
                    extension += filename[i];
                }
                //string extension = filename.Substring(filename.Length - 5);

                // Определяет количество архивов
                int count = (bt.Length + SIZE - 1) / SIZE;

                int cnt = 0;

                // Имя файла
                string flname = "";
                for (int i = filePath.Length + 1; i < filename.Length - extension.Length; i++)
                {
                    flname += filename[i];
                }

                // Создание архивов
                for (int i = 0; i < count; i++)
                {
                    using (FileStream zipToOpen = new FileStream(filePath + "\\" + flname + "@" + i + ".zip", FileMode.OpenOrCreate))
                    {
                        using (ZipArchive zip = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                        {
                            ZipArchiveEntry readmeEntry = zip.CreateEntry(i + extension);
                            using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                            {
                                long _byte_counter = SIZE;
                                while (_byte_counter > 0 && cnt < bt.Length)
                                {
                                    _byte_counter--;
                                    writer.BaseStream.WriteByte(bt[cnt]);
                                    progressBar.Value++;
                                    cnt++;
                                }
                            }
                        }
                    }
                }

                System.IO.File.WriteAllText(filePath + "\\" + flname + "_Info.txt", bt.Length.ToString() + "\r\n" + SIZE + "\r\n" + extension + "\r\n" + flname);
            }
        }
Exemple #28
0
        public void InvalidFiles()
        {
            Assert.Throws <InvalidDataException>(() => ZipFile.OpenRead(bad("EOCDmissing.zip")));
            using (TempFile testArchive = CreateTempCopyFile(bad("EOCDmissing.zip"), GetTestFilePath()))
            {
                Assert.Throws <InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
            }

            Assert.Throws <InvalidDataException>(() => ZipFile.OpenRead(bad("CDoffsetOutOfBounds.zip")));
            using (TempFile testArchive = CreateTempCopyFile(bad("CDoffsetOutOfBounds.zip"), GetTestFilePath()))
            {
                Assert.Throws <InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
            }

            using (ZipArchive archive = ZipFile.OpenRead(bad("CDoffsetInBoundsWrong.zip")))
            {
                Assert.Throws <InvalidDataException>(() => { var x = archive.Entries; });
            }

            using (TempFile testArchive = CreateTempCopyFile(bad("CDoffsetInBoundsWrong.zip"), GetTestFilePath()))
            {
                Assert.Throws <InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
            }

            using (ZipArchive archive = ZipFile.OpenRead(bad("numberOfEntriesDifferent.zip")))
            {
                Assert.Throws <InvalidDataException>(() => { var x = archive.Entries; });
            }
            using (TempFile testArchive = CreateTempCopyFile(bad("numberOfEntriesDifferent.zip"), GetTestFilePath()))
            {
                Assert.Throws <InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
            }

            //read mode on empty file
            using (var memoryStream = new MemoryStream())
            {
                Assert.Throws <InvalidDataException>(() => new ZipArchive(memoryStream));
            }

            //offset out of bounds
            using (ZipArchive archive = ZipFile.OpenRead(bad("localFileOffsetOutOfBounds.zip")))
            {
                ZipArchiveEntry e = archive.Entries[0];
                Assert.Throws <InvalidDataException>(() => e.Open());
            }

            using (TempFile testArchive = CreateTempCopyFile(bad("localFileOffsetOutOfBounds.zip"), GetTestFilePath()))
            {
                Assert.Throws <InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
            }

            //compressed data offset + compressed size out of bounds
            using (ZipArchive archive = ZipFile.OpenRead(bad("compressedSizeOutOfBounds.zip")))
            {
                ZipArchiveEntry e = archive.Entries[0];
                Assert.Throws <InvalidDataException>(() => e.Open());
            }

            using (TempFile testArchive = CreateTempCopyFile(bad("compressedSizeOutOfBounds.zip"), GetTestFilePath()))
            {
                Assert.Throws <InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
            }

            //signature wrong
            using (ZipArchive archive = ZipFile.OpenRead(bad("localFileHeaderSignatureWrong.zip")))
            {
                ZipArchiveEntry e = archive.Entries[0];
                Assert.Throws <InvalidDataException>(() => e.Open());
            }

            using (TempFile testArchive = CreateTempCopyFile(bad("localFileHeaderSignatureWrong.zip"), GetTestFilePath()))
            {
                Assert.Throws <InvalidDataException>(() => ZipFile.Open(testArchive.Path, ZipArchiveMode.Update));
            }
        }
Exemple #29
0
        private void encryptButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Remember:\n" +
                                "With Folder(s), Encryption Feature MUST compress it before encrypting.\n\n" +
                                "Do you want to continue?", "Encrypt", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }
            Account       receiver    = (Account)this.accountListBox.SelectedItem;
            List <string> successList = new List <string>();
            List <string> failureList = new List <string>();
            int           mode        = 0;

            mode += (this.compressCheckBox.Checked) ? 1 : 0;
            mode += (this.compressOneFileCheckBox.Checked) ? 1 : 0;
            EventHandler encryptEvent = null;

            switch (mode)
            {
            case 0:
                encryptEvent = new EventHandler(delegate(object o, EventArgs a)
                {
                    TreeNode node = (TreeNode)o;
                    string path   = node.FullPath;
                    try
                    {
                        if (File.Exists(path + ".enc"))
                        {
                            throw new Exception("Cannot encrypt because ENC file already exists.");
                        }
                        byte[] zipData = null;
                        if (node.BackColor == Color.Yellow)
                        {
                            using (MemoryStream memStream = new MemoryStream())
                            {
                                using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create))
                                    compressDirectory(path, Path.GetDirectoryName(path).Length + 1, archive);
                                zipData = memStream.ToArray();
                            }
                        }
                        else
                        {
                            zipData = File.ReadAllBytes(path);
                        }
                        byte[] data = new byte[zipData.Length + 33];
                        data[0]     = (byte)((node.BackColor == Color.Yellow) ? 1 : 0);
                        Buffer.BlockCopy(SHA256.Create().ComputeHash(zipData), 0, data, 1, 32);
                        Buffer.BlockCopy(zipData, 0, data, 33, zipData.Length);
                        File.WriteAllBytes(path + ".enc", DataEncryption.encrypt(receiver.getPublicKey(), data));
                        successList.Add(path);
                        node.Parent.Nodes.Add(node.Text + ".enc");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        failureList.Add(path);
                    }
                });
                doCheckedNodes(this.filesDirectoriesTreeView.Nodes, encryptEvent);
                break;

            case 1:
                encryptEvent = new EventHandler(delegate(object o, EventArgs a)
                {
                    TreeNode node = (TreeNode)o;
                    string path   = node.FullPath;
                    try
                    {
                        if (File.Exists(path + ".enc"))
                        {
                            throw new Exception("Cannot encrypt because ENC file already exists.");
                        }
                        byte[] zipData = null;
                        if (node.BackColor == Color.Yellow)
                        {
                            using (MemoryStream memStream = new MemoryStream())
                            {
                                using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create))
                                    compressDirectory(path, Path.GetDirectoryName(path).Length + 1, archive);
                                zipData = memStream.ToArray();
                            }
                        }
                        else
                        {
                            using (MemoryStream memStream = new MemoryStream())
                            {
                                using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create))
                                {
                                    ZipArchiveEntry entry = archive.CreateEntry(Path.GetFileName(path));
                                    using (BinaryWriter writer = new BinaryWriter(entry.Open()))
                                        writer.Write(File.ReadAllBytes(path));
                                }
                                zipData = memStream.ToArray();
                            }
                        }
                        byte[] data = new byte[zipData.Length + 33];
                        data[0]     = 1;
                        Buffer.BlockCopy(SHA256.Create().ComputeHash(zipData), 0, data, 1, 32);
                        Buffer.BlockCopy(zipData, 0, data, 33, zipData.Length);
                        File.WriteAllBytes(path + ".enc", DataEncryption.encrypt(receiver.getPublicKey(), data));
                        successList.Add(path);
                        node.Parent.Nodes.Add(node.Text + ".enc");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        failureList.Add(path);
                    }
                });
                doCheckedNodes(this.filesDirectoriesTreeView.Nodes, encryptEvent);
                break;

            case 2:
                try
                {
                    DirectoryInfo root = new DirectoryInfo(this.pathTextBox.Text);
                    if (File.Exists(root.FullName + "\\" + root.Name + ".enc"))
                    {
                        throw new Exception("Cannot encrypt because ENC file already exists.");
                    }
                    byte[] zipData = null;
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create))
                        {
                            encryptEvent = new EventHandler(delegate(object o, EventArgs a)
                            {
                                TreeNode node = (TreeNode)o;
                                string path   = node.FullPath;
                                if (node.BackColor == Color.Yellow)
                                {
                                    compressDirectory(path, Path.GetDirectoryName(path).Length + 1, archive);
                                }
                                else
                                {
                                    ZipArchiveEntry entry = archive.CreateEntry(Path.GetFileName(path));
                                    using (BinaryWriter writer = new BinaryWriter(entry.Open()))
                                        writer.Write(File.ReadAllBytes(path));
                                }
                            });
                            doCheckedNodes(this.filesDirectoriesTreeView.Nodes, encryptEvent);
                        }
                        zipData = memStream.ToArray();
                    }
                    byte[] data = new byte[zipData.Length + 33];
                    data[0] = 1;
                    Buffer.BlockCopy(SHA256.Create().ComputeHash(zipData), 0, data, 1, 32);
                    Buffer.BlockCopy(zipData, 0, data, 33, zipData.Length);
                    File.WriteAllBytes(root.FullName + "\\" + root.Name + ".enc", DataEncryption.encrypt(receiver.getPublicKey(), data));
                    this.filesDirectoriesTreeView.Nodes[0].Nodes.Add(root.Name + ".enc");
                    MessageBox.Show("Encrypt Success", "Encrypt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;

            default:
                MessageBox.Show("Encrypt Failure", "Encrypt Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string success = "Success: " + successList.Count.ToString() + " file(s) or folder(s)\n";

            foreach (string s in successList)
            {
                success += s + "\n";
            }
            string failure = "Failure: " + failureList.Count.ToString() + " file(s) or folder(s)\n";

            foreach (string s in failureList)
            {
                failure += s + "\n";
            }
            MessageBox.Show("Result:\n\n" + success + "\n" + failure, "Encrypt", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
            /***************************************************
                4.3.16  End of central directory record:
                end of central dir signature    4 bytes  (0x06054b50)
                number of this disk             2 bytes
                number of the disk with the
                start of the central directory  2 bytes
                total number of entries in the
                central directory on this disk  2 bytes
                total number of entries in
                the central directory           2 bytes
                size of the central directory   4 bytes
                offset of start of central
                directory with respect to
                the starting disk number        4 bytes
                .ZIP file comment length        2 bytes
                .ZIP file comment       (variable size)
            */
            /***************************************************
            File header:
                central file header signature   4 bytes  (0x02014b50)
                version made by                 2 bytes
                version needed to extract       2 bytes
                general purpose bit flag        2 bytes
                compression method              2 bytes
                last mod file time              2 bytes
                last mod file date              2 bytes
                crc-32                          4 bytes
                compressed size                 4 bytes
                uncompressed size               4 bytes
                file name length                2 bytes
                extra field length              2 bytes
                file comment length             2 bytes
                disk number start               2 bytes
                internal file attributes        2 bytes
                external file attributes        4 bytes
                relative offset of local header 4 bytes
                file name (variable size)
                extra field (variable size)
                file comment (variable size)
            */
            private List<ZipArchiveEntry> InflateDirectory()
            {
                _fileEntries = new List<ZipArchiveEntry>();

                BinaryReader reader = new BinaryReader(ZipStream);

                reader.BaseStream.Seek(-4, SeekOrigin.End);
                // skip back
                while (reader.ReadInt32() != END_CENTRAL_DIR_SIG)
                {
                    reader.BaseStream.Seek(-5, SeekOrigin.Current);
                }
                // skip over number of this disk, number of the disk with dir start, total number of entries on this disk
                reader.BaseStream.Seek(6, SeekOrigin.Current);
                short entryCount = reader.ReadInt16();
                int directorySize = reader.ReadInt32();
                int directoryStart = reader.ReadInt32();
                reader.BaseStream.Seek(directoryStart, SeekOrigin.Begin);
                bool doRebuild = false;

                for (int i = 0; i < entryCount; i++)
                {
                    if (reader.ReadInt32() == CENTRAL_FILE_HDR_SIG)
                    {
                        ZipArchiveEntry zipEntry = new ZipArchiveEntry();

                        reader.BaseStream.Seek(4, SeekOrigin.Current);
                        short flags = reader.ReadInt16();   // read general purpose bit flag

                        if ((flags & 8) > 0) //Silverlight doesn't like this format. We'll "fix it" further below
                        {
                            doRebuild = true;
                        }
                        // skip: compression method, last mod file time, last mod file date
                        reader.BaseStream.Seek(6, SeekOrigin.Current);

                        zipEntry.CRC32 = reader.ReadInt32();
                        zipEntry.CompressedLength = reader.ReadInt32();
                        zipEntry.Length = reader.ReadInt32();

                        short fileNameLength = reader.ReadInt16();
                        short extraFieldLength = reader.ReadInt16();
                        short fileCommentLength = reader.ReadInt16();

                        // skip disk number start, internal file attr, ext file attr
                        reader.BaseStream.Seek(8, SeekOrigin.Current);

                        zipEntry.FileStart = reader.ReadInt32();
                        zipEntry.Filename = new string(reader.ReadChars(fileNameLength));
                        _fileEntries.Add(zipEntry);

                        reader.BaseStream.Seek(extraFieldLength + fileCommentLength, SeekOrigin.Current);
                    }
                }
                if (doRebuild)
                {
                    // if file size is reported after the compressed data the filestream is unsupported by silverlight
                    MemoryStream newZipStream = new MemoryStream();
                    BinaryWriter writer = new BinaryWriter(newZipStream);

                    RebuildEntries(ref reader, ref writer);

                    // rewind
                    reader.BaseStream.Seek(directoryStart, SeekOrigin.Begin);
                    //Rebuild directory
                    RebuildDirectory(ref reader, ref writer);

                    writer.Write(reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)));
                    ZipStream = newZipStream; //Swap to use our newly cleaned stream
                }
                return _fileEntries;
            }
 public CookBookFile(string file)
 {
     this.Extension = GetFileExtension();
     this.FullName  = Path.GetFileName(file);
     this.Source    = null;
 }