Пример #1
0
        public static void DecryptDecompressDirectoryStructure(string fileToDecompress, BackupRestoreLocation location, string encryptionKey, ISimpleCredential credential, string tempLocation, string singleFile)
        {
            FileStream    fileStreamToRead = (FileStream)null;
            IFolderAccess folderAccess     = (IFolderAccess)null;
            string        str = string.Empty;

            try
            {
                if (location == BackupRestoreLocation.External)
                {
                    fileStreamToRead = new FileStream(fileToDecompress, FileMode.Open);
                }
                else
                {
                    string path = fileToDecompress;
                    if (!File.Exists(path))
                    {
                        FileInfo internalBackupFile = BackupFileServiceHelper.LatestInternalBackupFile;
                        if (internalBackupFile == null)
                        {
                            FileNotFoundException notFoundException = new FileNotFoundException("No internal or USB backup file available to restore.");
                            throw notFoundException;
                        }
                        path = internalBackupFile.DirectoryName + "\\" + internalBackupFile.Name;
                    }
                    fileStreamToRead = File.Open(path, FileMode.Open, FileAccess.Read);
                }
                if (fileStreamToRead != null && fileStreamToRead.Length == 0L)
                {
                    FileNotFoundException notFoundException = new FileNotFoundException("The file is missing or has a length of zero bytes.  It cannot be decrypted or unzipped.");
                    throw notFoundException;
                }
                if (!string.IsNullOrEmpty(encryptionKey))
                {
                    str = fileToDecompress;
                    BackupRestoreCompression.DecryptFile(fileStreamToRead, encryptionKey, str);
                    fileStreamToRead = File.Open(str, FileMode.Open, FileAccess.Read);
                }
                if (fileStreamToRead == null)
                {
                    return;
                }
                if (!string.IsNullOrEmpty(singleFile))
                {
                    BackupRestoreCompression.UnzipSingleFile(fileStreamToRead, tempLocation, singleFile);
                }
                else
                {
                    BackupRestoreCompression.UnzipFile(fileStreamToRead, tempLocation);
                }
            }
            finally
            {
                if (fileStreamToRead != null)
                {
                    fileStreamToRead.Close();
                    fileStreamToRead.Dispose();
                }
                if (folderAccess != null)
                {
                    folderAccess.Disconnect();
                    folderAccess.Dispose();
                }
                if (!string.IsNullOrEmpty(str) && File.Exists(str))
                {
                    File.Delete(str);
                }
            }
        }
Пример #2
0
        static void decrypt(string file, string key, string decryptedPath)
        {
            FileStream f = new FileStream(file, FileMode.Open);

            BackupRestoreCompression.DecryptFile(f, key, decryptedPath);
        }