在vb.net中如何使用SharpZipLib.dll来压缩/解压文件

tenkol 2010-10-07 02:54:15
如题,网上查了不少资料,都是#C的,无奈对#C不熟悉,自己尝试转换成vb,却出错!



Public Sub S_DeCompressFile(ByVal FileToUnZip As String, ByVal UnZipFile As String)

Try

Dim s As Zip.ZipInputStream = Nothing

s = New Zip.ZipInputStream(File.OpenRead(FileToUnZip))

s.Password = "test"

Dim fs As FileStream

fs = File.Create(UnZipFile)

Dim data(2048) As Byte

Dim size As Integer

size = s.Read(data, 0, size) '======>这里报错,无法从stream中读取

fs.Write(data, 0, size)

Catch ex As Exception

Throw ex

End Try

End Sub



请高手们给个压缩/解压的vb例子,谢谢
...全文
385 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
机器人 2010-10-07
  • 打赏
  • 举报
回复

Public Sub Unzip(ByVal strZipFileName As String, ByVal strUnzipFileName As String, Optional ByVal strPassword As String = "")
If Not File.Exists(strZipFileName) Then
Throw New Exception("Please specify a zip file")
End If

If Not Directory.Exists(strUnzipFileName) Then
Directory.CreateDirectory(strUnzipFileName)
End If

Dim strmZipInputStream As ZipInputStream = Nothing
Dim objZipEntry As ZipEntry = Nothing

Dim strFileName As String = ""
Dim strmFileStream As FileStream = Nothing

Try
strmZipInputStream = New ZipInputStream(File.OpenRead(strZipFileName))
If Not String.IsNullOrEmpty(strPassword) Then
strmZipInputStream.Password = strPassword
End If
objZipEntry = strmZipInputStream.GetNextEntry()
While objZipEntry IsNot Nothing
If Not String.IsNullOrEmpty(objZipEntry.Name) Then
strFileName = Path.Combine(strUnzipFileName, objZipEntry.Name)
If strFileName.EndsWith("\") OrElse strFileName.EndsWith("/") Then
Directory.CreateDirectory(strFileName)
objZipEntry = strmZipInputStream.GetNextEntry()
Continue While
End If

strmFileStream = File.Create(strFileName)
Dim intSize As Integer = 2048
Dim data(2048) As Byte
While True
intSize = strmZipInputStream.Read(data, 0, data.Length)
If intSize > 0 Then
strmFileStream.Write(data, 0, data.Length)
Else
Exit While
End If
End While
End If
objZipEntry = strmZipInputStream.GetNextEntry()
End While
Finally
If strmFileStream IsNot Nothing Then
strmFileStream.Close()
strmFileStream = Nothing
End If

If objZipEntry IsNot Nothing Then
objZipEntry = Nothing
End If

If strmZipInputStream IsNot Nothing Then
strmZipInputStream.Close()
strmZipInputStream = Nothing
End If

GC.Collect()
GC.Collect(1)

End Try
End Sub
tenkol 2010-10-07
  • 打赏
  • 举报
回复
本来想用.net自带的GZipStream类,不过这个类只能解决简单的压缩,解压操作,如果压缩文件带有密码,就不行了
tenkol 2010-10-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
http://topic.csdn.net/u/20091020/10/033d05ac-dd39-4754-8891-3698d0d603b5.html
[/Quote]

首先谢谢1楼,不过这种方法有点局限性,如果运行程序的电脑上没有安装winrar呢,以下附上C#的解压缩代码,望高人转换成vb代码!


public class UnZipClass
{
/**//// <summary>
/// 解压功能(解压压缩文件到指定目录)
/// </summary>
/// <param name="FileToUpZip">待解压的文件</param>
/// <param name="ZipedFolder">指定解压目标目录</param>
public static void UnZip(string FileToUpZip, string ZipedFolder,string Password)
{
if (!File.Exists(FileToUpZip))
{
return;
}

if (!Directory.Exists(ZipedFolder))
{
Directory.CreateDirectory(ZipedFolder);
}

ZipInputStream s = null;
ZipEntry theEntry = null;

string fileName;
FileStream streamWriter = null;
try
{
s = new ZipInputStream(File.OpenRead(FileToUpZip));
s.Password = Password;
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.Name != String.Empty)
{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
/**////判断文件路径是否是文件夹
if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
}

streamWriter = File.Create(fileName);
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;
}
}
}
}
}
finally
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter = null;
}
if (theEntry != null)
{
theEntry = null;
}
if (s != null)
{
s.Close();
s = null;
}
GC.Collect();
GC.Collect(1);
}
}
}
}




16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧