static void Create_Worker(ProgressBar bar, ArchiveWriter.Stats stats)
{
string src = Settings.Src;
string dst = Settings.Dst;
string refFile = Settings.Ref;
if (string.IsNullOrEmpty(src)) throw new Exception("You have to specify Src");
if (string.IsNullOrEmpty(dst)) throw new Exception("You have to specify Dst");
// The destination is directory
if (!Settings.CreateMulti && !dst.ToLowerInvariant().EndsWith(Settings.ArchiveExtension.ToLowerInvariant())) {
dst = Path.Combine(dst, Path.GetFileName(src) + Settings.ArchiveExtension);
}
if (Settings.RefRecent) {
string dir = Path.GetDirectoryName(dst);
if (Directory.Exists(dir)) {
refFile = GetMostRecentArchive(dir);
}
}
Reference reference = null;
if (!string.IsNullOrEmpty(refFile)) {
stats.Status = "Opening " + Path.GetFileName(refFile);
reference = new Reference(refFile);
}
List<string> verifycationList = new List<string>();
if (Settings.CreateMulti) {
// Multiple archives
string dstBase = dst;
foreach (string subDir in Directory.GetDirectories(src)) {
dst = Path.Combine(dstBase, Path.GetFileName(subDir)) + Settings.ArchiveExtension;
stats.Title = Path.GetFileName(dst);
string tmpName = dst + Settings.TmpExtension + new Random().Next();
ArchiveWriter archive = new ArchiveWriter(tmpName, stats);
archive.AddDir(subDir, reference);
reference = archive.Finish(Path.GetFileName(dst));
if (stats.Canceled) {
IOFile.Delete(tmpName);
break;
} else {
if (IOFile.Exists(dst)) {
IOFile.Delete(dst);
}
IOFile.Move(tmpName, dst);
verifycationList.Add(dst);
}
}
} else {
stats.Title = Path.GetFileName(dst);
string tmpName = dst + Settings.TmpExtension + new Random().Next();
Directory.CreateDirectory(Path.GetDirectoryName(tmpName));
ArchiveWriter archive = new ArchiveWriter(tmpName, stats);
archive.AddDir(src, reference);
archive.Finish(dst);
if (stats.Canceled) {
IOFile.Delete(tmpName);
} else {
if (IOFile.Exists(dst)) {
int suffix = 0;
string newDst;
do {
newDst = Path.Combine(Path.GetDirectoryName(dst), Path.GetFileNameWithoutExtension(dst) + (suffix == 0 ? string.Empty : ("-" + suffix.ToString())) + Path.GetExtension(dst));
suffix++;
} while (IOFile.Exists(newDst));
dst = newDst;
}
IOFile.Move(tmpName, dst);
verifycationList.Add(dst);
}
}
stats.EndTime = DateTime.Now;
// Free memory
reference = null;
bool allOk = true;
if (!stats.Canceled && Settings.Verify) {
ArchiveReader.Stats verficationStats = new ArchiveReader.Stats();
bar.Bind(verficationStats);
foreach (string filename in verifycationList) {
allOk = allOk && ArchiveReader.Extract(filename, null, verficationStats);
}
}
if (Settings.AutoQuit && allOk) bar.Close();
}