Example #1
0
        /// <summary>
        /// Decompresses a file using GZip decompression.
        /// </summary>
        /// <param name="inFile">Filepath of the file to be decompressed.</param>
        public static void Decompress(string inFile)
        {
            //Makes new FileInfo for the target file
            FileInfo fileToDecompress = new FileInfo(inFile);

            // Creates a FileStream containing the data from fileToDecompress
            using (FileStream originalFileStream = fileToDecompress.OpenRead()) {
                string currentFileName = fileToDecompress.FullName;
                string newFileName     = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

                // Creates the decompressed file stream
                using (FileStream decompressedFileStream = File.Create(newFileName)) {
                    // Creates the compression stream
                    using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress)) {
                        // Decompresses file
                        decompressionStream.CopyTo(decompressedFileStream);
                    }
                }

                // Updates FileOP to the decompressed file
                FileOP.LoadFile(FileOP.GetFile().Remove(FileOP.GetFile().Length - fileToDecompress.Extension.Length));
            }

            // Delete compressed file
            File.Delete(inFile);
        }
Example #2
0
        /// <summary>
        /// Opens up the dialog for creating a new password file
        /// Returns true upon success and false upon premature closing.
        /// </summary>
        public static bool CreateFile()
        {
            Stream myStream;
            //default to creating a .csv file
            SaveFileDialog save = new SaveFileDialog {
                Filter = "CSV |*.csv",
                Title  = "Save password DB"
            };

            //opens up the file explorer dialog menu and determines if the result is the ok button
            if (save.ShowDialog() == DialogResult.OK)
            {
                //as long as the stream is not null load the file name and optionally print it for testing purposes
                if ((myStream = save.OpenFile()) != null)
                {
                    FileOP.LoadFile(save.FileName);
                    FileOP.PrintFileName();
                    myStream.Close();
                }
                //Write the first line of the file to the file.
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(save.FileName, true)) {
                    file.WriteLine("Expiration Date,Title,UserName,Password,URL,Notes");
                    file.Close();
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #3
0
        /// <summary>
        /// Compresses a file using GZip compression.
        /// </summary>
        /// <param name="inFile">Filepath of the file to be compressed.</param>
        public static void Compress(string inFile)
        {
            //Makes new FileInfo for the target file
            FileInfo fileToCompress = new FileInfo(inFile);

            // Creates a File Stream containing the data in the fileToCompress
            using (FileStream originalFileStream = fileToCompress.OpenRead()) {
                // Check that the file is not hidden or already a .gz file
                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                {
                    // Creates a File Stream for the compressed file
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz")) {
                        // Creates the compression stream
                        using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress)) {
                            // Compresses file
                            originalFileStream.CopyTo(compressionStream);
                        }
                    }
                }
            }

            // Delete uncompressed file
            File.Delete(inFile);

            // Updates the file
            FileOP.LoadFile(FileOP.GetFile() + ".gz");
        }
Example #4
0
 /// <summary>
 /// Opens the dialog for opening a file allows opening of .gz files
 /// </summary>
 public static bool SelectFile()
 {
     using (OpenFileDialog openFileDialog = new OpenFileDialog()) {
         openFileDialog.InitialDirectory = "c:\\";
         openFileDialog.Filter           = "gz file (*.gz)|*.gz";
         openFileDialog.RestoreDirectory = true;
         //if the result is ok load the file
         if (openFileDialog.ShowDialog() == DialogResult.OK)
         {
             //set the path of the file that is selected
             FileOP.LoadFile(openFileDialog.FileName);
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Example #5
0
        /// <summary>
        /// Writes all the password entries to the CSV file.
        /// </summary>
        public static void WriteToFile(DataTable dataTable)
        {
            string origFilePath = GetFile();

            // Creation of temp file to rewrite file
            string tempFile = "temp_file.csv";

            using (FileStream fs = File.Create(tempFile)) { }

            // Write all of the password entries in the data table to the temp file
            using (StreamWriter writer = new StreamWriter(tempFile)) {
                using (CsvWriter csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture)) {
                    // Setting the map so that entries get written into the correct columns
                    csvWriter.Context.RegisterClassMap <PasswordEntryMap>();

                    // Creates a list for the password entries and adds the entries to it
                    List <PasswordEntry> passwordEntries = new List <PasswordEntry>()
                    {
                    };
                    foreach (DataRow row in dataTable.Rows)
                    {
                        PasswordEntry passwordEntry = new PasswordEntry(row["expiration date"].ToString(), row["title"].ToString(), row["username"].ToString(),
                                                                        row["password"].ToString(), row["url"].ToString(), row["notes"].ToString());
                        passwordEntries.Add(passwordEntry);
                    }

                    // Casting the password list to an IEnumerable
                    IEnumerable <PasswordEntry> enumData = passwordEntries;

                    // Write the entries to the CSV
                    csvWriter.WriteRecords(enumData);
                }
            }

            // Replace the original file with the temp file
            File.Delete(origFilePath);
            File.Move(tempFile, origFilePath);

            // Update the file currently being used by the program
            FileOP.LoadFile(origFilePath);
        }