Example #1
0
        public static void EncryptCompressDirectoryStructure(string directoryToCompress, string filePathToCompressTo, BackupRestoreLocation location, ISimpleCredential credential, string versionInfo, string encryptionKey)
        {
            string str1;

            if (location != BackupRestoreLocation.External)
            {
                str1 = filePathToCompressTo;
            }
            else
            {
                str1 = filePathToCompressTo;
            }
            string        str2         = str1;
            IFolderAccess folderAccess = (IFolderAccess)null;

            try
            {
                string path = "zip";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                using (ZipFile zipFile = new ZipFile())
                {
                    zipFile.TempFileFolder = path;
                    zipFile.AddDirectory(directoryToCompress, string.Empty);
                    zipFile.Comment = versionInfo;
                    zipFile.Save(str2);
                }
                if (string.IsNullOrEmpty(encryptionKey) || location != BackupRestoreLocation.External)
                {
                    return;
                }

                using (FileStream file = new FileStream(filePathToCompressTo, FileMode.CreateNew))
                {
                    using (CryptoStream encryptionStream = DataProtection.GetEncryptionStream((Stream)file, encryptionKey))
                    {
                        FileStream fileStream = (FileStream)null;
                        try
                        {
                            fileStream = new FileStream(str2, FileMode.Open);
                            byte[] buffer = new byte[fileStream.Length];
                            int    count;
                            while ((count = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                encryptionStream.Write(buffer, 0, count);
                            }
                        }
                        finally
                        {
                            if (fileStream != null)
                            {
                                fileStream.Close();
                                fileStream.Dispose();
                            }
                            File.Delete(str2);
                        }
                    }
                }
            }
            finally
            {
                if (folderAccess != null)
                {
                    folderAccess.Disconnect();
                    folderAccess.Dispose();
                }
            }
        }
Example #2
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);
                }
            }
        }