gpt4 book ai didi

c# - 在亚马逊 S3 中创建 zip 文件夹

转载 作者:行者123 更新时间:2023-11-30 22:19:51 26 4
gpt4 key购买 nike

我必须在 amazon S3 中创建一个文件夹。现在必须转换 zip 文件中的那个文件夹。我已经使用 DotNetZip Liberary 转换为 .zip 文件。这是链接

http://dotnetzip.codeplex.com/wikipage?title=CS-Examples

public void ConvertToZip(string directoryToZip, string zipFileName)
{
try
{

using (client = DisposableAmazonClient())
{
var sourDir = new S3DirectoryInfo(client, bucket, directoryToZip);

var destDir = new S3DirectoryInfo(client, bucket, CCUrlHelper.BackupRootFolderPhysicalPath);

using (var zip = new ZipFile())
{
zip.AddDirectory(sourDir.FullName); // recurses subdirectories
zip.Save(Path.Combine(destDir.FullName, zipFileName));
}
}

logger.Fatal("Successfully converted to Zip.");
}
catch (Exception ex)
{
logger.Error("Error while converting to zip. Error : " + ex.Message);
}
}

当我运行代码时,它显示错误“不支持给定路径的格式。”

最佳答案

S3DirectoryInfo 模拟目录结构,但它不是实际的目录结构,并且 DotNetZip 不知道如何处理指向 S3 中对象的字符串。为了让你这样做,你将不得不下载文件,将它们压缩,然后上传生成的 zip 文件。下面是一些示例代码,展示了如何做到这一点。

class Program{    static void Main(string[] args)    {        var zipFilename = @"c:\temp\data.zip";        var client = new AmazonS3Client();        S3DirectoryInfo rootDir = new S3DirectoryInfo(client, "norm-ziptest");        using (var zip = new ZipFile())        {            zip.Name = zipFilename;            addFiles(zip, rootDir, "");        }

    // Move local zip file to S3
var fileInfo = rootDir.GetFile("data.zip");
fileInfo.MoveFromLocal(zipFilename);
}

static void addFiles(ZipFile zip, S3DirectoryInfo dirInfo, string archiveDirectory)
{

foreach (var childDirs in dirInfo.GetDirectories())
{
var entry = zip.AddDirectoryByName(childDirs.Name);
addFiles(zip, childDirs, archiveDirectory + entry.FileName);
}

foreach (var file in dirInfo.GetFiles())
{
using (var stream = file.OpenRead())
{
zip.AddEntry(archiveDirectory + file.Name, stream);

// Save after adding the file because to force the
// immediate read from the S3 Stream since
// we don't want to keep that stream open.
zip.Save();
}
}
}

关于c# - 在亚马逊 S3 中创建 zip 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15338070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com