Use java.util develop a C#.net zip tools

王朝c#·作者佚名  2006-01-09
宽屏版  字体: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有