zipoutputstream在文件夹之前添加根驱动器路径

时间:2017-07-21 06:49:17

标签: c# asp.net zip compression

我正在使用zioutpoustream创建一个zip文件,但是当我打开zip文件时,它首先显示根路径,如D:LocalDisk,而不是压缩文件夹。 这是我创建zip的代码

public void StartZip(string directory, string zipfile_path)
{
    // the directory you need to zip
    DirectoryInfo Dictiontory = new DirectoryInfo(directory);
    //to get sub directories
    DirectoryInfo[] Dir = Dictiontory.GetDirectories();
    string[] filenames;
    // path which the zip file built in
    ZipOutputStream s = new ZipOutputStream(File.Create(zipfile_path));
    foreach (DirectoryInfo folder in Dir)
    {
        filenames = Directory.GetFiles(directory+"/"+folder);
        foreach (string filename in filenames)
        {
            FileStream fs = File.OpenRead(filename);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            ZipEntry entry = new ZipEntry(filename);
            s.PutNextEntry(entry);
            s.Write(buffer, 0, buffer.Length);
            fs.Close();
        }
    }
    s.SetLevel(5);
    s.Finish();
    s.Close();
}

1 个答案:

答案 0 :(得分:0)

如果有人遇到同样的问题,请使用此选项:

ZipFile.CreateFromDirectory(directory, zipfile_path);