2009-07-02 57 views
2

確定提取特定的文件,SharpZipLib〜如何從一個zip

我有文件的列表(這只是只包含文件名的SourceFile對象),那麼我想拉這些特定的文件了拉鍊和甩掉他們進入臨時目錄,以便以後可以分發它們。

我想出了這一點,但我對接下來該怎麼繼續不確定..

private List<string> ExtractSelectedFiles() 
{ 
List<SourceFile> zipFilePaths = new List<SourceFile>(); 
List<string> tempFilePaths = new List<string>(); 

if (!File.Exists(this.txtSourceSVNBuildPackage.Text)) { return tempFilePaths; }; 

FileStream zipFileStream = File.OpenRead(this.txtSourceSVNBuildPackage.Text); 
ZipInputStream inStream = new ZipInputStream(zipFileStream); 

foreach (SourceFile currentFile in _selectedSourceFiles) 
{ 
    bool getNextEntry = true; 

    while (getNextEntry) 
    { 
      ZipEntry entry = inStream.GetNextEntry(); 

     getNextEntry = (entry != null); 

       if (getNextEntry) 
      { 
      if (fileType == ".dll") 
      { 
       if (sourcefile.Name == Path.GetFileName(entry.Name)) 
       { 
       //Extract file into a temp directory somewhere 

       //tempFilePaths.Add("extractedfilepath") 
       } 
      } 
      } 
      } 
     } 

    return tempFilePaths; 
} 

FYI:

public class SourceFile 
{ 
    public string Name { get; set; } //ex. name = "Fred.dll" 
} 

回答

2

確定..想通之後,我得到了我會更新你所有一起是我需要的缺失部分。

//in the code somewhere above: 
string tempDirectory = Environment.GetEnvironmentVariable("TEMP"); 
string createPath = tempDirectory + "\\" + Path.GetFileName(entry.Name); 


//my missing piece.. 
//Extract file into a temp directory somewhere 
FileStream streamWriter = File.Create(createPath); 

int size = 2048; 
byte[] data = new byte[2048]; 
while (true) 
{ 
    size = inStream.Read(data, 0, data.Length); 
    if (size > 0) 
    { 
     streamWriter.Write(data, 0, size); 
    } 
    else 
    { 
     break; 
    } 
} 

streamWriter.Close(); 
相關問題