2013-03-11 77 views
1

我必須在亞馬遜S3中創建一個文件夾。現在必須在zip文件中轉換該文件夾。我已經使用DotNetZip Liberary轉換爲.zip文件。這裏是鏈接爲在亞馬遜S3中創建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); 
    } 
} 

當我運行它顯示的錯誤代碼「給出的路徑的格式不支持。」

回答

2

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(); 
     } 
    } 
} 

}