.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Use java.util develop a C#.net zip tools

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

1. Step 1: add reference to vjslib.dll under C:\$WIDOWS DIRECTORY$\Microsoft.NET\Framework\$VERSION NUMBER $\

2. using
  java.util.zip;

3. code

/// <summary>
/// Zip single file
/// </summary>
/// <param name="FilePath">original file path : string like "c:\\intrafinity\\web\\scorm\\"</param>
/// <param name="FileName">original file name: string like "win2000.gif"</param>
/// <param name="ZipFileName">zipped file : string like "c:\\intrafinity\\web\\scorm\\new.zip"</param>

public static void ZipFile(string FilePath, string FileName, string ZipFileName)
{

ZipOutputStream os = new ZipOutputStream(new java.io.FileOutputStream(ZipFileName));
ZipEntry ze = new ZipEntry(FileName);
ze.setMethod(
ZipEntry.DEFLATED);
os.putNextEntry(ze);

java.io.FileInputStream fs = new java.io.FileInputStream(string.Concat(FilePath,FileName));
sbyte[] buff = new sbyte[1024];
int n = 0;

while ((n = fs.read(buff, 0, buff.Length)) > 0)
{os.write(buff, 0, n);}

fs.close();
os.closeEntry();
os.close();

}

 

/// <summary>
/// Zip folder
/// </summary>
/// <param name="FolderName">folder name : string like "c:\\intrafinity\\web\\scorm\\"</param>
/// <param name="ZipFileName">zipped file: string like "c:\\intrafinity\\web\\s.zip"</param>
public static void ZipFolder(string FolderName, string ZipFileName)
{

ZipOutputStream os = new ZipOutputStream(new java.io.FileOutputStream(ZipFileName));
ZipFolder(FolderName, ZipFileName, "", os);
os.closeEntry();
os.close();

}

public static void ZipFolder(string FolderName, string ZipFileName, string Addon, ZipOutputStream os)
{

string[] strs1 = Directory.GetFiles(FolderName);
string[] strs2 = Directory.GetDirectories(FolderName);

for (int i = 0; i < (int)strs1.Length; i++)
{
string str1 = strs1[i];
FileInfo fileInfo = new FileInfo(str1);
ZipEntry ze = new ZipEntry(string.Concat(Addon,fileInfo.Name));
ze.setMethod(
ZipEntry.DEFLATED);
os.putNextEntry(ze);

java.io.FileInputStream fs = new java.io.FileInputStream(string.Concat(FolderName, fileInfo.Name));
sbyte[] buff = new sbyte[1024];
int n = 0;

while ((n = fs.read(buff, 0, buff.Length)) > 0)
{
os.write(buff, 0, n);
}

fs.close();
}

for (int j = 0; j < (int)strs2.Length; j++)
{
string str2 = strs2[j];
DirectoryInfo directoryInfo = new DirectoryInfo(str2);

ZipFolder(string.Concat(FolderName, directoryInfo.Name, "\\"), ZipFileName, string.Concat(Addon, directoryInfo.Name,"\\"), os);
}

}


4. Notice: .NET Framework 1.1 and 1.0 have some bug with vjslib.dll, the zipped file will have some head errors, but the content is fine, it can still be opened by winzip and winrar, but you will have some problem when open the zipped file by using wjslib.dll in your code. Ironic?
Framework 2.0 fixed the problem.

5. another solution is http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/default.aspx




相关文章

相关软件