SharpZipLib.dll 子目录下的文件解压到了根目录

.Net技术 针尖舞 9年前 (2015-08-31) 2221次浏览 0个评论
 

 

为什么这个解压程序把所有的子目录下的文件都解压到根目录下了那?/**//// <summary> 

 /// 解压文件  

using System; 

using System.Text; 

using System.Collections; 

using System.IO; 

using System.Diagnostics; 

using System.Runtime.Serialization.Formatters.Binary; 

using System.Data; 

 

using ICSharpCode.SharpZipLib.BZip2; 

using ICSharpCode.SharpZipLib.Zip; 

using ICSharpCode.SharpZipLib.Zip.Compression; 

using ICSharpCode.SharpZipLib.Zip.Compression.Streams; 

using ICSharpCode.SharpZipLib.GZip; 

 

namespace DeCompression 

 public class UnZipClass 

 {    

  public void UnZip(string[] args) 

  { 

   ZipInputStream s = new ZipInputStream(File.OpenRead(args[0])); 

   

   ZipEntry theEntry; 

   while ((theEntry = s.GetNextEntry()) != null)  

   { 

    

          string directoryName = Path.GetDirectoryName(args[1]); 

    string fileName      = Path.GetFileName(theEntry.Name); 

    

    //生成解压目录 

    Directory.CreateDirectory(directoryName); 

    

    if (fileName != String.Empty)  

    {    

     //解压文件到指定的目录 

     FileStream streamWriter = File.Create(args[1]+theEntry.Name); 

     

     int size = 2048; 

     byte[] data = new byte[2048]; 

     while (true)  

     { 

      size = s.Read(data, 0, data.Length); 

      if (size > 0)  

      { 

       streamWriter.Write(data, 0, size); 

      }  

      else  

      { 

       break; 

      } 

     } 

     

     streamWriter.Close(); 

    } 

   } 

   s.Close(); 

  } 

 } 

/**//// <summary> 

 /// 调用源码 

 /// </summary> 

 

     private void button2_Click(object sender, System.EventArgs e) 

  { 

   string []FileProperties=new string[2]; 

   FileProperties[0]=”C:\zip\test.zip”;//待解压的文件 

   FileProperties[1]=”C:\unzipped”;//解压后放置的目标目录 

   UnZipClass UnZc=new UnZipClass(); 

   UnZc.UnZip(FileProperties); 

  } 

 

————

 

FileProperties[0]=”C:\zip\test.zip”;//待解压的文件 

   FileProperties[1]=”C:\unzipped”;//解压后放置的目标目录 

这不行吧,

   FileProperties[0]=@”C:\zip\test.zip”;//待解压的文件 

   FileProperties[1]=@”C:\unzipped”;//解压后放置的目标目录 

 

FileProperties[1]=@”C:\unzipped”;试试

 

alien54155415() 

跟哪个没关系,我改过了

 

/// <summary>

/// 解压指定的zip文件

/// </summary>

/// <param name=”filename”>文件全路径名称</param>

/// <param name=”targetPath”>解压后文件保存的路径名全称,必须带 “\”</param>

public static void GetZipEntry(string filename,string targetPath)

{

// 创建读取Zip文件对象

ZipInputStream zos = new ZipInputStream(File.OpenRead(filename));

// Zip文件中的每一个文件

ZipEntry theEntry;

// 循环读取Zip文件中的每一个文件

while ((theEntry = zos.GetNextEntry()) != null) 

{            

string directoryName = targetPath;

string fileName      = Path.GetFileName(theEntry.Name);

            

// create directory

if(!Directory.Exists(directoryName))

Directory.CreateDirectory(directoryName);

            

if (fileName != String.Empty) 

{

// 解压文件

FileStream streamWriter = File.Open(directoryName + fileName,FileMode.Create);

               

int size = 2048;

byte[] data = new byte[2048];

while (true) 

{

// 写入数据

size = zos.Read(data, 0, data.Length);

if (size > 0) 

{

streamWriter.Write(data, 0, size);

else 

{

break;                             }

}

                

streamWriter.Close();

}

}

zos.Close();

}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SharpZipLib.dll 子目录下的文件解压到了根目录
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!