2012-02-25 3 views
1

私は問題があります。その理由から、私はzipファイルへの純粋なC#ソリューションを開発しています。これは古典的なASPサイトのためのもので、私はあなたのdllをサーバーに登録します。私はすでに他のサードパーティライブラリをテストしました...サードパーティのDLLを使用しないZipの作成。 Part URIを取得するには、スラッシュエラーで始まる必要があります

私は次のエラーが表示されています: パーツURIはスラッシュで始まる必要があります。

ここで私はグーグルグーグルで構築することができた実装です。

PackagePart newFilePackagePart = zipFilePackage.CreatePart(partURI, contentType, CompressionOption.Normal); 

[TestMethod] 
public void ArchiveFile() 
{ 
    string dir = "\\\\filebox01\\data\\test"; 
    string file = "text.xls"; 

    ZipClassic zip = new ZipClassic(); 

    bool ok = zip.ArchiveFile(dir, file, "singleFileArchive.zip"); 
    Assert.IsTrue(ok); 
} 

主な方法:

public bool ArchiveFile(string fileDir, string fileToArchive, string newArchiveFileName) 
{ 
    FileSystem fso = new FileSystem(); 

    bool ok = !String.IsNullOrWhiteSpace(fileDir) && 
       !String.IsNullOrWhiteSpace(fileToArchive) && 
       fso.FileExists(Path.Combine(fileDir, fileToArchive)) && 
       fileToArchive.Contains("."); 


    if (ok) 
    { 
     if (!String.IsNullOrWhiteSpace(newArchiveFileName)) 
     { 
      if (!newArchiveFileName.ToLower().Contains(".zip")) 
       newArchiveFileName = String.Concat(newArchiveFileName, ".zip"); 
     } 
     else 
     { 
      string filePart = fileToArchive.Substring(0, fileToArchive.LastIndexOf(".", System.StringComparison.Ordinal)); 
      newArchiveFileName = String.Concat(filePart, ".zip"); 
     } 

     //if archve file already exists then delete it 
     if (fso.FileExists(Path.Combine(fileDir, newArchiveFileName))) 
      ok = fso.FileDelete(Path.Combine(fileDir, newArchiveFileName)); 
    } 

    if (ok) 
    { 
     Impersonate impersonate = new Impersonate(); 
     impersonate.DoImpersonate(); 

     Package zipFile = Package.Open(Path.Combine(fileDir, newArchiveFileName), FileMode.OpenOrCreate, FileAccess.ReadWrite); 
     FileInfo file = new FileInfo(Path.Combine(fileDir, fileToArchive)); 
     AddFileToZip(file, zipFile); 
     zipFile.Close(); 

     impersonate.Dispose(); 

     ok = fso.FileExists(Path.Combine(fileDir, newArchiveFileName)); 
    } 
    return ok; 
} 

protected void AddFileToZip(FileInfo file, Package zipFilePackage) 
{ 
    string physicalfilePath = file.FullName; 

    //Check for file existing. If file does not exists, 
    //then add in the report to generate at the end of the process. 
    if (File.Exists(physicalfilePath)) 
    { 
     string fileName = Path.GetFileName(physicalfilePath); 

     // Remove the section of the path that has "root defined" 
     physicalfilePath = physicalfilePath.Replace("./", ""); 

     // remove space from the file name and replace it with "_" 
     physicalfilePath = physicalfilePath.Replace(fileName, fileName.Replace(" ", "_")); 

     try 
     { 
      //Define URI for this file that needs to be added within the Zip file. 
      Uri partURI = new Uri(physicalfilePath, UriKind.Relative); 
      string contentType = GetFileContentType(physicalfilePath); 
      PackagePart newFilePackagePart = zipFilePackage.CreatePart(partURI, contentType, CompressionOption.Normal); 
      byte[] fileContent = File.ReadAllBytes(physicalfilePath); 
      newFilePackagePart.GetStream().Write(fileContent, 0, fileContent.Length); 
     } 
     catch (Exception ex) 
     { 
      throw new ApplicationException("Unable to archive: " + ex.Message); 
     } 
    } 
} 

protected string GetFileContentType(string path) 
{ 
    string contentType = System.Net.Mime.MediaTypeNames.Application.Zip; 
    switch (Path.GetExtension(path).ToLower()) 
    { 
     case (".xml"): 
     { 
      contentType = System.Net.Mime.MediaTypeNames.Text.Xml; 
      break; 
     } 

     case (".txt"): 
     { 
      contentType = System.Net.Mime.MediaTypeNames.Text.Plain; 
      break; 
     } 

     case (".rtf"): 
     { 
      contentType = System.Net.Mime.MediaTypeNames.Application.Rtf; 
      break; 
     } 

     case (".gif"): 
     { 
      contentType = System.Net.Mime.MediaTypeNames.Image.Gif; 
      break; 
     } 

     case (".jpeg"): 
     { 
      contentType = System.Net.Mime.MediaTypeNames.Image.Jpeg; 
      break; 
     } 

     case (".tiff"): 
     { 
      contentType = System.Net.Mime.MediaTypeNames.Image.Tiff; 
      break; 
     } 

     case (".pdf"): 
     { 
      contentType = System.Net.Mime.MediaTypeNames.Application.Pdf; 
      break; 
     } 

     case (".doc"): 
     case (".docx"): 
     case (".ppt"): 
     case (".xls"): 
     { 
      contentType = System.Net.Mime.MediaTypeNames.Text.RichText; 
      break; 
     } 
    } 

    return contentType; 
} 
+0

'新しいウリ(physicalfilePath、UriKind.Relativeは)'何をお返ししましたか?ヒント:@を使用する逐語的な文字列リテラルは読みやすくなります: 'string dir = @" \\ filebox01 \ data \ test ";'。 – HABO

+0

タイトルに "C#"というプレフィックスを付けないでください。それがタグのためのものです。 –

+0

これを変更しました。文字列dir = @ "\\ filebox01 \ data \ test \ text.xls"; Uri partURI =新しいUri(physicalfilePath、UriKind.Relative); {\\ filebox01 \ data \ test \ text.xls}を返します。私がpartURIを展開すると、OriginalStringに対して上記の値が得られ、IsAbsoluteUri = falseになります。残りのアイテムはすべて、次の例外をスローします。System.InvalidOperationException –

答えて

2

問題がpartUriを作成していた私のエラーは、この方法ではライン上で "AddFileToZip" です。私は完全なパスを、ファイル名であったはずです。

Uri partUri = PackUriHelper.CreatePartUri(new Uri(String.Concat(@".\", fileName), UriKind.Relative)); 

このリンクは問題を解決するのに役立ちました。

http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx

関連する問題