SharpZipLib ~ How to extract specific files from a zip

11,669

ok.. figured I'd update you all after I got together the missing piece I needed.

//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();
Share:
11,669
KevinDeus
Author by

KevinDeus

C# JQuery nunit WCF Subversion SharpSVN TFS RoombaSCI CreateOI Framework ChessMangler

Updated on June 04, 2022

Comments

  • KevinDeus
    KevinDeus almost 2 years

    Ok,

    I have a list of files (SourceFile objects which just contain the filename only) then I want to pull those specific files out of a zip and dump them into a temp directory so I can distribute them later.

    I came up with this, but I am unsure on how to proceed next..

    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"
    }