闡述VS2003壓縮代碼的有關(guān)常識
昨天到今天搞了一整天的VS2003壓縮代碼,我都快崩潰了! 一看到那些代碼,腦袋頓時(shí)就像爆炸一樣,所以有了許多的問題出現(xiàn),還好,我一個(gè)個(gè)把他記錄下來了,同時(shí),在相關(guān)論壇上找了一些相關(guān)的解決辦法,分享一下,供大家相互學(xué)習(xí)交流
1、首先從這里下載0.84版本的VS2003壓縮代碼及示例碼。
2、下載下來之后你發(fā)現(xiàn)它沒有VS2003的解決方案文件,沒有關(guān)系。你可以自己建立,首先新建一個(gè)ZipUnzip的解決方案,然后,將上面經(jīng)過解壓縮之后的所有文件及目錄COPY到你的解決方案所在的目錄下。 #t#
3、在VS2003解決方案資源管理器(一般是在右上方中部點(diǎn)的位置)中點(diǎn)擊顯示所有文件按鈕,然后可以見到很多“虛”的圖標(biāo)、文件及文件夾等,可以一次選擇它們,然后包含進(jìn)項(xiàng)目中。
4、編譯,***使用Release選項(xiàng),編譯完成之后你可以在\bin\Release\看到ZipUnzip.dll的類了。如果你編譯時(shí)報(bào)錯(cuò),說什么AssemblyKeyFile之類的,你可以使用強(qiáng)命名工具新建一個(gè),也可以將AssemblyInfo.cs中[assembly: AssemblyKeyFile("。。。。。")]改成:[assembly: AssemblyKeyFile("")] (不推薦這樣做)。
5、新建一個(gè)WEBFORM項(xiàng)目,添加ZipUnzip.dll類的引用,然后添加如下文件及內(nèi)容:
- using System;
- using System.IO;
- using ICSharpCode.SharpZipLib.Zip;
- using ICSharpCode.SharpZipLib.GZip;
- using ICSharpCode.SharpZipLib.BZip2;
- using ICSharpCode.SharpZipLib.Checksums;
- using ICSharpCode.SharpZipLib.Zip.Compression;
- using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
- namespace WebZipUnzip
- {
- public class AttachmentUnZip
- {
- public AttachmentUnZip()
- {}
- public static void UpZip(string zipFile)
- {
- string []FileProperties=new string[2];
- FileProperties[0]=zipFile;//待解壓的文件
- FileProperties[1]=zipFile.Substring(0,zipFile.LastIndexOf("\\")+1);//解壓后放置的目標(biāo)目錄
- UnZipClass UnZc=new UnZipClass();
- UnZc.UnZip(FileProperties);
- }
- }
- }
- // ---------------------------------------------
- // 2. UnZipClass.cs
- // ---------------------------------------------
- using System;
- using System.IO;
- using ICSharpCode.SharpZipLib.Zip;
- using ICSharpCode.SharpZipLib.GZip;
- using ICSharpCode.SharpZipLib.BZip2;
- using ICSharpCode.SharpZipLib.Checksums;
- using ICSharpCode.SharpZipLib.Zip.Compression;
- using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
- namespace WebZipUnzip
- {
- public class UnZipClass
- {
- ///
- /// 解壓文件
- ///
- /// 包含要解壓的文件名和要解壓到的目錄名數(shù)組
- public void UnZip(string[] args)
- {
- ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
- try
- {
- 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]+fileName);
- int size = 2048;
- byte[] data = new byte[2048];
- while (true)
- {
- ssize = s.Read(data, 0, data.Length);
- if (size > 0)
- {
- streamWriter.Write(data, 0, size);
- }
- else
- {
- break;
- }
- }
- streamWriter.Close();
- }
- }
- s.Close();
- }
- catch(Exception eu)
- {
- throw eu;
- }
- finally
- {
- s.Close();
- }
- }//end UnZip
- public static bool UnZipFile(string file, string dir)
- {
- try
- {
- if (!Directory.Exists(dir))
- Directory.CreateDirectory(dir);
- string fileFullName = Path.Combine(dir,file);
- ZipInputStream s = new ZipInputStream(File.OpenRead( fileFullName ));
- ZipEntry theEntry;
- while ((theEntry = s.GetNextEntry()) != null)
- {
- string directoryName = Path.GetDirectoryName(theEntry.Name);
- string fileName = Path.GetFileName(theEntry.Name);
- if (directoryName != String.Empty)
- Directory.CreateDirectory( Path.Combine(dir, directoryName));
- if (fileName != String.Empty)
- {
- FileStream streamWriter = File.Create( Path.Combine(dir,theEntry.Name) );
- int size = 2048;
- byte[] data = new byte[2048];
- while (true)
- {
- ssize = s.Read(data, 0, data.Length);
- if (size > 0)
- {
- streamWriter.Write(data, 0, size);
- }
- else
- {
- break;
- }
- }
- streamWriter.Close();
- }
- }
- s.Close();
- return true;
- }
- catch (Exception)
- {
- throw;
- }
- }
- }//end UnZipClass
- }
此方案解決了文件名中文字的問題,目錄VS2003壓縮代碼問題,至于整個(gè)文件夾批量上傳并壓縮成一個(gè)WINZIP壓縮包的問題,沒有時(shí)間解決了,各位如有解決方案,不妨共享一下。