/// <summary> /// Reads all entries from existing archive log file /// </summary> public void ReadAllEntries() { // Read entries FileStream stream = File.OpenRead(this.path); while (stream.Position < (stream.Length - 1)) { // Read line List <byte> encodedLine = new List <byte>(); while (true) { byte b = (byte)stream.ReadByte(); if (b == '\n') { break; } else { encodedLine.Add(b); } } // Process line into an entry and store ArchiveLogEntry entry = new ArchiveLogEntry( Encoding.UTF8.GetString(encodedLine.ToArray()) ); this.entries.Add(entry.path, entry); } // Mark as not empty this.empty = false; }
/// <summary> /// Writes an entry into the archive log /// </summary> /// <param name="entry"></param> public void WriteEntry(ArchiveLogEntry entry) { // Write entry to file byte[] logLine = Encoding.UTF8.GetBytes( String.Format("{0}\n", entry.ToString()) ); this.stream.Write(logLine, 0, logLine.Length); // Store entry this.entries.Add(entry.path, entry); // Update empty status this.empty = false; }
/// <summary> /// Writes a file to archive /// </summary> /// <param name="dirPath">Path to the root directory of the file being archived</param> /// <param name="filePath">Relative path within the root directory of the file being archived</param> /// <returns>File status</returns> public ArchiveLogFileArchivingStatus WriteFile(string dirPath, string filePath) { if (this.zip == null) { throw new Exception("Can't write to archive before opening it! Call .Create() first!"); } else { // Get file info string fullPath = String.Format("{0}{1}", dirPath, filePath); FileInfo info = new FileInfo(fullPath); ArchiveLogEntry currentEntry = new ArchiveLogEntry() { path = filePath, mtime = (info.CreationTimeUtc > info.LastWriteTimeUtc ? info.CreationTimeUtc : info.LastWriteTimeUtc).ToFileTimeUtc(), size = info.Length, }; // Check if file changed since last backup ArchiveLogEntry previousEntry = (this.previousLog != null ? this.previousLog.FindEntry(filePath) : null); if (previousEntry != null) { if ((previousEntry.status != ArchiveLogFileArchivingStatus.DELETED) && (currentEntry.mtime == previousEntry.mtime) && (currentEntry.size == previousEntry.size)) { // File unchanged - set status and copy archive path currentEntry.status = ArchiveLogFileArchivingStatus.UNCHANGED; currentEntry.archive = previousEntry.archive; } else if (previousEntry.status == ArchiveLogFileArchivingStatus.DELETED) { // File recreated - set status and archive path currentEntry.status = ArchiveLogFileArchivingStatus.CREATED; currentEntry.archive = this.archiveName; } else { // File changed - set status and archive path currentEntry.status = ArchiveLogFileArchivingStatus.CHANGED; currentEntry.archive = this.archiveName; } } else { // File created - set status and archive path currentEntry.status = ArchiveLogFileArchivingStatus.CREATED; currentEntry.archive = this.archiveName; } // Check if file needs to be archived if ((currentEntry.status == ArchiveLogFileArchivingStatus.CREATED) || (currentEntry.status == ArchiveLogFileArchivingStatus.CHANGED)) { // Add file entry to archive zip.CreateEntryFromFile(fullPath, filePath, Archive.compression); // Updated empty status this.empty = false; } // Log file entry information this.currentLog.WriteEntry(currentEntry); // Return file status return(currentEntry.status); } }