Exemplo n.º 1
0
        protected override Task<HttpResponseMessage> CreateDirectoryGetResponse(DirectoryInfo info, string localFilePath)
        {
            HttpResponseMessage response = Request.CreateResponse();
            using (var ms = new MemoryStream())
            {
                using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true))
                {
                    foreach (FileSystemInfo fileSysInfo in info.EnumerateFileSystemInfos())
                    {
                        DirectoryInfo directoryInfo = fileSysInfo as DirectoryInfo;
                        if (directoryInfo != null)
                        {
                            zip.AddDirectory(new DirectoryInfoWrapper(directoryInfo), fileSysInfo.Name);
                        }
                        else
                        {
                            // Add it at the root of the zip
                            zip.AddFile(fileSysInfo.FullName, String.Empty);
                        }
                    }
                }
                response.Content = ms.AsContent();
            }

            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

            // Name the zip after the folder. e.g. "c:\foo\bar\" --> "bar"
            response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(Path.GetDirectoryName(localFilePath)) + ".zip";
            return Task.FromResult(response);
        }
Exemplo n.º 2
0
 public static void Zip(string zipFileName, string zipInArchiveRoot, params string[] files)
 {
     if (files == null || files.Length == 0)
     {
         //LogUtility.LogWarning("There are no input compress file!");
         return;
     }
     FileUtility.Delete(zipFileName);
     DirectoryUtility.CreateDirectory(Path.GetDirectoryName(zipFileName));
     try
     {
         using (ZipArchive zipArchive = ZipArchive.Create(zipFileName, string.Empty))
         {
             string inputFile;
             string fileNameInArchive;
             for (int i = 0; i < files.Length; i++)
             {
                 inputFile = files[i];
                 if (!inputFile.Contains(zipInArchiveRoot))
                 {
                     //LogUtility.LogWarning("The file isn't in the root directory!");
                     continue;
                 }
                 fileNameInArchive = PathUtility.Trim(zipInArchiveRoot, inputFile);
                 zipArchive.AddFile(compressMode, inputFile, fileNameInArchive, string.Empty);
             }
             //ZipArchive.Close();
         }
     }
     catch (Exception ex)
     {
         //LogUtility.LogError("[ZipFile.Zip]" + ex.ToString());
     }
 }
Exemplo n.º 3
0
        public void AddFileToArchiveCreatesEntryUnderDirectory(string fileName, string content)
        {
            // Arrange
            var stream = new MemoryStream();
            var zip = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true);
            var fileInfo = CreateFile(fileName, content);

            // Act
            zip.AddFile(fileInfo, "");

            // Assert
            zip.Dispose();
            zip = new ZipArchive(ReOpen(stream));
            Assert.Equal(1, zip.Entries.Count);
            AssertZipEntry(zip, fileName, content);
        }
Exemplo n.º 4
0
 private void AddFilesToZip(ZipArchive zip)
 {
     foreach (var path in _paths)
     {
         if (Directory.Exists(path))
         {
             var dir = new DirectoryInfo(path);
             if (path.EndsWith(Constants.LogFilesPath, StringComparison.Ordinal))
             {
                 foreach (var info in dir.GetFileSystemInfos())
                 {
                     var directoryInfo = info as DirectoryInfo;
                     if (directoryInfo != null)
                     {
                         // excluding FREB as it contains user sensitive data such as authorization header
                         if (!info.Name.StartsWith("W3SVC", StringComparison.OrdinalIgnoreCase))
                         {
                             zip.AddDirectory(directoryInfo, _tracer, Path.Combine(dir.Name, info.Name));
                         }
                     }
                     else
                     {
                         zip.AddFile((FileInfo)info, _tracer, dir.Name);
                     }
                 }
             }
             else
             {
                 zip.AddDirectory(dir, _tracer, Path.GetFileName(path));
             }
         }
         else if (File.Exists(path))
         {
             zip.AddFile(path, _tracer, String.Empty);
         }
     }
 }
        public void AddFileInUseTests()
        {
            // Arrange
            var fileName = @"x:\test\temp.txt";
            var exception = new IOException("file in use");
            var stream = new MemoryStream();
            var zip = new ZipArchive(stream, ZipArchiveMode.Create);
            var tracer = new Mock<ITracer>();
            var file = new Mock<FileInfoBase>();

            // setup
            file.Setup(f => f.OpenRead())
                .Throws(exception);
            file.SetupGet(f => f.FullName)
                .Returns(fileName);
            tracer.Setup(t => t.Trace(It.IsAny<string>(), It.IsAny<IDictionary<string, string>>()))
                  .Callback((string message, IDictionary<string, string> attributes) =>
                  {
                      Assert.Contains("error", attributes["type"]);
                      Assert.Contains(fileName, attributes["text"]);
                      Assert.Contains("file in use", attributes["text"]);
                  });

            // Act
            zip.AddFile(file.Object, tracer.Object, String.Empty);
            zip.Dispose();

            // Assert
            tracer.Verify(t => t.Trace(It.IsAny<string>(), It.IsAny<IDictionary<string, string>>()), Times.Once());
            zip = new ZipArchive(ReOpen(stream));
            Assert.Equal(0, zip.Entries.Count);
        }