【问题标题】:SharpZipLib ~ How to extract specific files from a zip [duplicate]SharpZipLib ~ 如何从 zip 中提取特定文件 [重复]
【发布时间】:2009-07-02 18:44:55
【问题描述】:

好的,

我有一个文件列表(仅包含文件名的 SourceFile 对象)然后我想从 zip 中提取这些特定文件并将它们转储到临时目录中,以便以后分发它们。

我想出了这个,但我不确定下一步如何进行..

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

仅供参考:

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

【问题讨论】:

    标签: c# extract unzip sharpziplib compression


    【解决方案1】:

    好吧..我想在我找到我需要的缺失部分后更新你们所有人。

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

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2015-11-17
      • 1970-01-01
      相关资源
      最近更新 更多