/// <summary> /// Extracts the given archive. /// </summary> /// <param name="archive">the archive to extract</param> /// <param name="overwrite">true if existing files should be overwritten</param> /// <param name="append">true if only new files should be extracted if the user is already registered</param> private static void Extract(FileSystemInfo archive, bool overwrite, bool append) { string username = Path.GetFileNameWithoutExtension(archive.Name); bool userExists = FileStore.FileExists(Resources.UserDirectory, username + ".json"); if (userExists && !append) { return; } using (ZipArchive zipArchive = ZipFile.OpenRead(archive.FullName)) { foreach (ZipArchiveEntry entry in zipArchive.Entries) { string destination = FileStore.GetAbsolutePath(entry.FullName); if (File.Exists(destination) && overwrite) { entry.ExtractToFile(destination, true); } else if (!File.Exists(destination)) { FileStore.CreateDirectory(Path.GetDirectoryName(entry.FullName)); entry.ExtractToFile(destination, false); } } } }
/// <summary> /// Creates an archive for the specified user containing all that user's data. /// </summary> /// <param name="username">a username</param> private static void Archive(string username) { FileStore.CreateDirectory(Resources.ArchiveDirectory); if (FileStore.FileExists(Resources.ArchiveDirectory, username + ".zip")) { return; } string archivePath = FileStore.GetAbsolutePath(Resources.ArchiveDirectory, username + ".zip"); using (var fileStream = new FileStream(archivePath, FileMode.CreateNew)) using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true)) { ArchiveUserData(archive, username); ArchiveCurrentProjectList(archive, username); ArchiveOldProjectLists(archive, username); ArchiveProjectCode(archive, username); } }