2012-02-27 95 views
0

我在此路徑「D:\ Nagaraj \ Dotnet \ Zipfile \ Zipfile \ Filebuild \ Hi.zip」中創建了zip文件。但在此Hi.zip文件創建文件夾「D:\ Nagaraj \ Dotnet \ Zipfile \ Zipfile \ Filebuild \ Hi」之後添加了2個文本文件顯示....所以我需要刪除文件夾路徑在zip文件中...我正在使用下面的代碼和sharpziplib庫...提前感謝。如何刪除zip文件中的文件夾路徑。我只想在zip文件中使用txt文件

*enter code here* 
StartZip("D:/Nagaraj/Dotnet/Zipfile/Zipfile/Filebuild/Hi/", Server.MapPath("Filebuild/Hi.zip")); 

public void StartZip(string directory, string zipFileName) 
{ 
    ZipFile z = ZipFile.Create(zipFileName); 
    z.BeginUpdate(); 
    string[] filenames = Directory.GetFiles(directory); 

    foreach (string filename in filenames) 
     { 
     z.Add(filename); 
     string s = Path.GetFileName(filename); 
     } 
     z.CommitUpdate(); 
     z.Close(); 
} 

回答

0

使用下面的代碼,你可以刪除的文件夾路徑:

FileInfo hfinfo = null; 
hfinfo = new FileInfo(HFileName.ToString()); 
if (!hfinfo.Exists) { 
    hfinfo.Create(); 
} 
HGetTData.ToString().TrimEnd(); 

sw.Write(HGetTData.ToString()); 
HGetTData.Length = 0; 

sw.Flush(); 

sw.Close(); 
sw.Dispose(); 

z.NameTransform = new ZipNameTransform(hosttmppath); 
z.BeginUpdate(); 
z.Add(HFileName.ToString(), fname[i] + ".txt"); 
z.CommitUpdate(); 

FileInfo theFile = new FileInfo(HFileName.ToString()); 
if (theFile.Exists) { 
    File.Delete(HFileName.ToString()); 
} 
相關問題