Skip to content

Commit e134c52

Browse files
committed
Add dryrun argument to zf sample. Other minor tidying as well.
1 parent 75f6a04 commit e134c52

1 file changed

Lines changed: 144 additions & 79 deletions

File tree

samples/cs/zf/zf.cs

Lines changed: 144 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace ICSharpCode.SharpZipLib.Samples.CS.ZF
2727
{
2828

2929
/// <summary>
30-
/// A command line archiver using the <see cref="ZipFile"/> class from SharpZipLib compression library
30+
/// A command line archiver using the <see cref="ZipFile"/> class from the SharpZipLib compression library
3131
/// </summary>
3232
public class ZipFileArchiver
3333
{
@@ -156,6 +156,10 @@ bool SetArgs(string[] args)
156156
operation_ = Operation.Test;
157157
break;
158158

159+
case "-dryrun":
160+
dryRun_ = true;
161+
break;
162+
159163
case "-env":
160164
ShowEnvironment();
161165
break;
@@ -433,7 +437,8 @@ void ShowHelp()
433437

434438
Console.Out.WriteLine("--add Add files to archive");
435439
Console.Out.WriteLine("--create Create new archive");
436-
Console.Out.WriteLine("--data Test archive data"); Console.Out.WriteLine("--delete Delete files from archive");
440+
Console.Out.WriteLine("--data Test archive data");
441+
Console.Out.WriteLine("--delete Delete files from archive");
437442
Console.Out.WriteLine("--encoding=codepage|name Set code page for encoding by name or number");
438443
Console.Out.WriteLine("--extract{=dir} Extract archive contents to dir(default .)");
439444
Console.Out.WriteLine("--help Show this help");
@@ -526,7 +531,7 @@ void ListArchiveContents(ZipFile zipFile, FileInfo fileInfo)
526531
Console.Out.WriteLine(headerUnderline);
527532
Console.Out.WriteLine(
528533
"{0,-12} {1,10:0} {2,3}% {3,10:0} {4,10:d} {4:hh:mm:ss}",
529-
entryCount.ToString() + " entries", totalSize, GetCompressionRatio(totalCompressedSize, totalSize), fileInfo.Length, fileInfo.LastWriteTime);
534+
entryCount + " entries", totalSize, GetCompressionRatio(totalCompressedSize, totalSize), fileInfo.Length, fileInfo.LastWriteTime);
530535
}
531536
}
532537

@@ -671,84 +676,133 @@ void Create(ArrayList fileSpecs)
671676
/// <returns>True if operation is successful; false otherwise.</returns>
672677
bool ExtractFile(Stream inputStream, ZipEntry theEntry, string targetDir)
673678
{
674-
// try and sort out the correct place to save this entry
675-
string entryFileName;
676-
677-
if (Path.IsPathRooted(theEntry.Name))
678-
{
679-
string workName = Path.GetPathRoot(theEntry.Name);
680-
workName = theEntry.Name.Substring(workName.Length);
681-
entryFileName = Path.Combine(Path.GetDirectoryName(workName), Path.GetFileName(theEntry.Name));
682-
}
683-
else
684-
{
685-
entryFileName = theEntry.Name;
686-
}
679+
if (inputStream == null)
680+
{
681+
throw new ArgumentNullException("inputStream");
682+
}
683+
684+
if (theEntry == null)
685+
{
686+
throw new ArgumentNullException("theEntry");
687+
}
688+
689+
if (!theEntry.IsFile)
690+
{
691+
throw new ArgumentException("Not a file", "theEntry");
692+
}
693+
694+
if (targetDir == null)
695+
{
696+
throw new ArgumentNullException("targetDir");
697+
}
698+
699+
// try and sort out the correct place to save this entry
700+
701+
bool result = true;
702+
bool process = theEntry.Name.Length > 0;
703+
704+
if (!process)
705+
{
706+
// A fuller program would generate or prompt for a filename here...
707+
if (!silent_)
708+
{
709+
Console.WriteLine("Ignoring empty name");
710+
}
711+
712+
return false;
713+
}
714+
715+
string entryFileName;
716+
717+
if (Path.IsPathRooted(theEntry.Name))
718+
{
719+
string workName = Path.GetPathRoot(theEntry.Name);
720+
workName = theEntry.Name.Substring(workName.Length);
721+
entryFileName = Path.Combine(Path.GetDirectoryName(workName), Path.GetFileName(theEntry.Name));
722+
}
723+
else
724+
{
725+
entryFileName = theEntry.Name;
726+
}
727+
728+
string targetName = Path.Combine(targetDir, entryFileName);
729+
string fullDirectoryName = Path.GetDirectoryName(Path.GetFullPath(targetName));
687730

688-
string targetName = Path.Combine(targetDir, entryFileName);
689-
string fullPath = Path.GetDirectoryName(Path.GetFullPath(targetName));
690731
#if TEST
691732
Console.WriteLine("Decompress targetfile name " + entryFileName);
692-
Console.WriteLine("Decompress targetpath " + fullPath);
733+
Console.WriteLine("Decompress targetpath " + fullDirectoryName);
693734
#endif
694-
695-
// Could be an option or parameter to allow failure or try creation
696-
if (Directory.Exists(fullPath) == false)
697-
{
698-
try
699-
{
700-
Directory.CreateDirectory(fullPath);
701-
}
702-
catch
703-
{
704-
return false;
705-
}
706-
}
707-
else if (overwriteFiles == Overwrite.Prompt)
708-
{
709-
if (File.Exists(targetName))
710-
{
711-
Console.Write("File " + targetName + " already exists. Overwrite? ");
712-
713-
string readValue;
714-
try
715-
{
716-
readValue = Console.ReadLine();
717-
}
718-
catch
719-
{
720-
readValue = null;
721-
}
722-
723-
if ( (readValue == null) || (readValue.ToLower() != "y") )
724-
{
725-
return true;
726-
}
727-
}
728-
}
729-
730-
if (entryFileName.Length > 0)
735+
736+
// Could be an option or parameter to allow failure or try creation
737+
if (process)
738+
{
739+
if (Directory.Exists(fullDirectoryName) == false)
740+
{
741+
try
742+
{
743+
Directory.CreateDirectory(fullDirectoryName);
744+
}
745+
catch (Exception ex)
746+
{
747+
if (!silent_)
748+
{
749+
Console.Write("Exception creating directory '{0}' - {1}", fullDirectoryName, ex.Message);
750+
}
751+
752+
result = false;
753+
process = false;
754+
}
755+
}
756+
else if (overwriteFiles == Overwrite.Prompt)
757+
{
758+
if (File.Exists(targetName))
759+
{
760+
Console.Write("File " + targetName + " already exists. Overwrite? ");
761+
762+
string readValue;
763+
try
764+
{
765+
readValue = Console.ReadLine();
766+
}
767+
catch
768+
{
769+
readValue = null;
770+
}
771+
772+
if ((readValue == null) || (readValue.ToLower() != "y"))
773+
{
774+
process = false;
775+
}
776+
}
777+
}
778+
}
779+
780+
if (process)
731781
{
732782
if ( !silent_ )
733783
{
734784
Console.Write("{0}", targetName);
735785
}
736-
using (FileStream outputStream = File.Create(targetName))
737-
{
738-
StreamUtils.Copy(inputStream, outputStream, GetBuffer());
739-
}
740-
741-
if (restoreDateTime_)
742-
{
743-
File.SetLastWriteTime(targetName, theEntry.DateTime);
744-
}
745-
746-
if ( !silent_ )
786+
787+
if (!dryRun_)
788+
{
789+
using (FileStream outputStream = File.Create(targetName))
790+
{
791+
StreamUtils.Copy(inputStream, outputStream, GetBuffer());
792+
}
793+
794+
if (restoreDateTime_)
795+
{
796+
File.SetLastWriteTime(targetName, theEntry.DateTime);
797+
}
798+
}
799+
800+
if ( !silent_ )
747801
{
748802
Console.WriteLine(" OK");
749803
}
750804
}
751-
return true;
805+
return result;
752806
}
753807

754808
/// <summary>
@@ -768,17 +822,23 @@ bool DecompressArchive(string fileName, string targetDir)
768822
zf.Password = password_;
769823
foreach ( ZipEntry entry in zf )
770824
{
771-
if ( entry.IsFile )
772-
{
773-
ExtractFile(zf.GetInputStream(entry), entry, targetDir);
774-
}
775-
else
776-
{
777-
if ( !silent_ )
778-
{
779-
Console.WriteLine("Skipping {0}", entry.Name);
780-
}
781-
}
825+
if (entry.IsFile)
826+
{
827+
if (!ExtractFile(zf.GetInputStream(entry), entry, targetDir))
828+
{
829+
if (!silent_)
830+
{
831+
Console.WriteLine("Extraction failed {0}", entry.Name);
832+
}
833+
}
834+
}
835+
else
836+
{
837+
if (!silent_)
838+
{
839+
Console.WriteLine("Skipping {0}", entry.Name);
840+
}
841+
}
782842
}
783843

784844
if ( !silent_ )
@@ -1399,6 +1459,11 @@ public static void Main(string[] args)
13991459
/// </summary>
14001460
bool testData_;
14011461

1462+
/// <summary>
1463+
/// Dont extract or compress just display what would be done - testing
1464+
/// </summary>
1465+
private bool dryRun_;
1466+
14021467
/// <summary>
14031468
/// The currently active <see cref="ZipFile"/>.
14041469
/// </summary>

0 commit comments

Comments
 (0)