protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { if (DestinationLocation != null) { DestinationLocation.Clear(); } if (SourceLocation != null) { SourceLocation.Clear(); } FileList.Clear(); FileList = null; ArchiveMemoryStream.Dispose(); if (File.Exists(WorkingArchive)) { File.Delete(WorkingArchive); } } disposedValue = true; } }
/// <summary> /// Updates the files in the archive with the files found in specified folder. /// </summary> /// <param name="SourceFolder">The folder specified.</param> public void UpdateArchive(string SourceFolder) { List <String> SourceFiles = new List <String>(); SourceFiles = PullFiles(SourceFolder); foreach (string file in SourceFiles) { var entry = ArchiveMemoryStream.GetEntry(file.Replace(SourceFolder + GetFolderCharacter(), "")); if (entry != null) { string originalFileHash; string targetFileHash; using (var algo = SHA256.Create()) { var archivedFile = entry.Open(); var targetFile = File.OpenRead(file); var originalFileSignature = algo.ComputeHash(archivedFile); originalFileHash = BitConverter.ToString(originalFileSignature); originalFileSignature = algo.ComputeHash(targetFile); targetFileHash = BitConverter.ToString(originalFileSignature); archivedFile.Dispose(); targetFile.Close(); } if (originalFileHash != targetFileHash) { RemoveFileFromList(entry.FullName); AddFileToList(SourceFolder, file.Replace(SourceFolder + GetFolderCharacter(), "")); } } else { AddFileToList(SourceFolder, file.Replace(SourceFolder + GetFolderCharacter(), "")); } } }
/// <summary> /// Adds files from a specific folder. The folder relative location is used to avoid messing up the folder structure. /// </summary> /// <param name="sourceLocation">The location of the source folder</param> /// <param name="SourceFolderRelativeLocation">The relatice location of the source folder.</param> public void AddFilesFromAFolder(string sourceLocation, string SourceFolderRelativeLocation) { var tempList = PullFiles(Path.Combine(sourceLocation, SourceFolderRelativeLocation)); foreach (string fileEntry in tempList) { ArchiveMemoryStream.CreateEntryFromFile(fileEntry, fileEntry.Replace(sourceLocation + GetFolderCharacter(), ""), CompressionLevel.NoCompression); FileList.Add(fileEntry.Replace(sourceLocation + GetFolderCharacter(), "")); } }
/// <summary> /// Adds files from a specific folder. /// </summary> /// <param name="source">The location of the source folder.</param> public void AddFilesFromAFolder(string source) { var tempList = PullFiles(source); foreach (string fileEntry in tempList) { ArchiveMemoryStream.CreateEntryFromFile(fileEntry, fileEntry.Replace(source + GetFolderCharacter(), ""), CompressionLevel.NoCompression); FileList.Add(fileEntry.Replace(source + GetFolderCharacter(), "")); } }
/// <summary> /// Removes a specified file from the archive. /// </summary> /// <param name="fileLocation">The file you want to remove. This must be in a relative path. ("folder1/file.extension")</param> public void RemoveFileFromList(string fileLocation) { var entry = ArchiveMemoryStream.GetEntry(fileLocation); if (entry == null) { return; } entry.Delete(); FileList.Remove(fileLocation); }
/// <summary> /// Adds a specified file to the list. This method is better suited for preserving the folder structure. /// </summary> /// <param name="source">The base folder that holds the file.</param> /// <param name="fileRelativeLocation">The relative location of the file.</param> public void AddFileToList(string source, string fileRelativeLocation) { FileList.Add(fileRelativeLocation); ArchiveMemoryStream.CreateEntryFromFile(Path.Combine(source, fileRelativeLocation), fileRelativeLocation, CompressionLevel.NoCompression); }
/// <summary> /// Adds a specified file to the list. This method only works if the file is in the source folder. /// </summary> /// <param name="fileLocation">The location of the file.</param> public void AddFileToList(string fileLocation) { FileList.Add(fileLocation.Replace(SourceLocation + GetFolderCharacter(), "")); ArchiveMemoryStream.CreateEntryFromFile(fileLocation, fileLocation.Replace(SourceLocation + GetFolderCharacter(), ""), CompressionLevel.NoCompression); }
/// <summary> /// Extracts a specified file to a location specified. /// </summary> /// <param name="entry">The file you want to extract. This must be in a relative path. ("folder1\\file.extension")</param> /// <param name="exportLocation">The location where the file will be extracted to.</param> public void ExtractFile(string entry, string exportLocation) { var candidateFile = ArchiveMemoryStream.GetEntry(entry); candidateFile.ExtractToFile(exportLocation, true); }
/// <summary> /// Closes the archive. /// </summary> public void CloseArchive() { ArchiveMemoryStream.Dispose(); EncryptedPackageUtilities.CompressArchive(WorkingArchive, DestinationLocation.ToString(), Password); }