public void BuildReturnsZipFileWithFiles ()
    {
      var zipBuilder = new ZipFileBuilder();
      zipBuilder.Progress += ((sender, e) => { });
      zipBuilder.AddFile (new FileInfoWrapper (new FileInfo (_file1.FileName)));
      zipBuilder.AddFile (new FileInfoWrapper (new FileInfo (_file2.FileName)));

      var zipFileName = Path.GetTempFileName();

      using (zipBuilder.Build (zipFileName))
      {
      }

      var expectedFiles = new List<string> { Path.GetFileName (_file1.FileName), Path.GetFileName (_file2.FileName) };
      CheckUnzippedFiles (zipFileName, expectedFiles);
    }
    public void SetFileProcessingAction_Retry ()
    {
      var zipBuilder = new ZipFileBuilder();
      zipBuilder.Progress += ((sender, e) => { });
      zipBuilder.Error += ((sender, e) => zipBuilder.FileProcessingRecoveryAction = FileProcessingRecoveryAction.Retry);

      var fileInfoStub = MockRepository.GenerateStub<IFileInfo>();

      zipBuilder.AddFile (new FileInfoWrapper (new FileInfo (_file1.FileName)));
      fileInfoStub.Stub (stub => stub.FullName).Return (_file2.FileName);
      fileInfoStub.Stub (stub => stub.Name).Return (Path.GetFileName (_file2.FileName));
      fileInfoStub.Stub (stub => stub.Length).Return (_file2.Length);
      zipBuilder.AddFile (fileInfoStub);

      fileInfoStub.Expect (mock => mock.Open (FileMode.Open, FileAccess.Read, FileShare.Read)).Throw (new IOException()).Repeat.Once();

      var fileInfo = new FileInfoWrapper (new FileInfo (_file2.FileName));
      var stream = fileInfo.Open (FileMode.Open, FileAccess.Read, FileShare.Read);
      fileInfoStub.Expect (mock => mock.Open (FileMode.Open, FileAccess.Read, FileShare.Read)).Return (stream);

      fileInfoStub.Stub (mock => mock.Directory).Return (new DirectoryInfoWrapper (new DirectoryInfo (Path.GetDirectoryName (_file2.FileName))));

      var zipFileName = Path.GetTempFileName();
      using (zipBuilder.Build (zipFileName))
      {
      }
      var expectedFiles = new List<string> { Path.GetFileName (_file1.FileName), Path.GetFileName (_file2.FileName) };
      CheckUnzippedFiles (zipFileName, expectedFiles);
    }
    public void BuildThrowsAbortExceptionUponCancel ()
    {
      var zipBuilder = new ZipFileBuilder ();
      zipBuilder.Progress += ((sender, e) => { e.Cancel = e.CurrentFileValue > 1000; });
      zipBuilder.AddFile (new FileInfoWrapper (new FileInfo (_file1.FileName)));

      var zipFileName = Path.GetTempFileName ();

      using (zipBuilder.Build (zipFileName))
      {
      }
    }
    public void SetFileProcessingAction_Ignore ()
    {
      var zipBuilder = new ZipFileBuilder();
      zipBuilder.Progress += ((sender, e) => { });

      var fileInfoMock = MockRepository.GenerateMock<IFileInfo>();

      fileInfoMock.Expect (mock => mock.FullName).Return (@"C:\fileName");
      fileInfoMock.Expect (mock => mock.Open (FileMode.Open, FileAccess.Read, FileShare.Read)).Throw (new IOException());
      fileInfoMock.Stub (mock => mock.Directory).Return (new DirectoryInfoWrapper (new DirectoryInfo (@"C:\")));

      zipBuilder.AddFile (new FileInfoWrapper (new FileInfo (_file1.FileName)));
      zipBuilder.AddFile (fileInfoMock);

      zipBuilder.Error += ((sender, e) => zipBuilder.FileProcessingRecoveryAction = FileProcessingRecoveryAction.Ignore);
      var zipFileName = Path.GetTempFileName();
      using (zipBuilder.Build (zipFileName))
      {
      }

      var expectedFiles = new List<string> { Path.GetFileName (_file1.FileName) };
      CheckUnzippedFiles (zipFileName, expectedFiles);
    }
    public void SetFileProcessingRecoveryAction_Abort ()
    {
      var zipBuilder = new ZipFileBuilder();
      zipBuilder.Progress += ((sender, e) => { });

      var fileInfoMock = MockRepository.GenerateMock<IFileInfo>();
      fileInfoMock.Expect (mock => mock.FullName).Return (@"C:\fileName");
      fileInfoMock.Expect (mock => mock.Open (FileMode.Open, FileAccess.Read, FileShare.Read)).Throw (new IOException());
      fileInfoMock.Stub (mock => mock.Directory).Return (new DirectoryInfoWrapper (new DirectoryInfo (@"C:\")));

      zipBuilder.AddFile (fileInfoMock);

      zipBuilder.Error += ((sender, e) => zipBuilder.FileProcessingRecoveryAction = FileProcessingRecoveryAction.Abort);

      var zipFileName = Path.GetTempFileName();
      try
      {
        using (zipBuilder.Build (zipFileName))
        {
        }
      }
      finally
      {
        FileUtility.DeleteAndWaitForCompletion (zipFileName);
      }
    }
    public void BuildReturnsZipFileWithFileWithUmlaut ()
    {
      string fileWithUmlautInName = Path.Combine (Path.GetTempPath(), "NameWithÄ.txt");
      File.WriteAllText (fileWithUmlautInName, "Hello World!");

      try
      {
        var zipBuilder = new ZipFileBuilder();
        zipBuilder.Progress += ((sender, e) => { });
        zipBuilder.AddFile (new FileInfoWrapper (new FileInfo (fileWithUmlautInName)));

        var zipFileName = Path.GetTempFileName();

        using (zipBuilder.Build (zipFileName))
        {
        }

        var expectedFiles = new List<string> { Path.GetFileName (fileWithUmlautInName) };
        CheckUnzippedFiles (zipFileName, expectedFiles);
      }
      finally
      {
        FileUtility.DeleteAndWaitForCompletion (fileWithUmlautInName);
      }
    }
    public void BuildReturnsZipFileWithEmptyFile_FromInMemoryFile ()
    {
      var zipBuilder = new ZipFileBuilder();
      zipBuilder.Progress += ((sender, e) => { });
      zipBuilder.AddFile (new InMemoryFileInfo ("TheFile", new MemoryStream (new byte[0]), null, DateTime.Today, DateTime.Today, DateTime.Today));

      var zipFileName = Path.GetTempFileName();

      using (zipBuilder.Build (zipFileName))
      {
      }

      var expectedFiles = new[] { "TheFile" };
      CheckUnzippedFiles (zipFileName, expectedFiles);
    }
    public void BuildReturnsZipFileWithEmptyFile_WithDiskFile ()
    {
      using (var fileEmpty = new TempFile ())
      {
        var zipBuilder = new ZipFileBuilder();
        zipBuilder.Progress += ((sender, e) => { });
        zipBuilder.AddFile (new FileInfoWrapper (new FileInfo (fileEmpty.FileName)));

        var zipFileName = Path.GetTempFileName();

        using (zipBuilder.Build (zipFileName))
        {
        }

        var expectedFiles = new[] { Path.GetFileName (fileEmpty.FileName) };
        CheckUnzippedFiles (zipFileName, expectedFiles);
      }
    }