当前位置: 首页>>代码示例>>C#>>正文


C# ZipArchive.AddFile方法代码示例

本文整理汇总了C#中System.IO.Compression.ZipArchive.AddFile方法的典型用法代码示例。如果您正苦于以下问题:C# ZipArchive.AddFile方法的具体用法?C# ZipArchive.AddFile怎么用?C# ZipArchive.AddFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Compression.ZipArchive的用法示例。


在下文中一共展示了ZipArchive.AddFile方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateDirectoryGetResponse

        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);
        }
开发者ID:WeAreMammoth,项目名称:kudu-obsolete,代码行数:31,代码来源:ZipController.cs

示例2: AddFileToArchiveCreatesEntryUnderDirectory

        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);
        }
开发者ID:GregPerez83,项目名称:kudu,代码行数:16,代码来源:ZipArchiveExtensionFacts.cs

示例3: AddFilesToZip

 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);
         }
     }
 }
开发者ID:hackmp,项目名称:kudu,代码行数:37,代码来源:DiagnosticsController.cs

示例4: AddFileInUseTests

        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);
        }
开发者ID:NorimaConsulting,项目名称:kudu,代码行数:32,代码来源:ZipArchiveExtensionFacts.cs


注:本文中的System.IO.Compression.ZipArchive.AddFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。