本文整理汇总了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);
}
示例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);
}
示例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);
}
}
}
示例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);
}