Zip (unzip) & asp.net

M

mavrick_101

Hi,

Is there a free component for zipping and unzipping files using c# in ASP.net?

Thnx
 
G

Gregory A. Beamer \(Cowboy\) - MVP

There is the SharpZipLib that is open sourced. I would aim that direction
before picking up on other samples out there. It is a nice full library.

You can do some compression with the stuff in .NET, but the wrapper classes
out there make things easier.
 
M

mavrick_101

Sloan and Gregory,

Thanks for your responses. I will check both.

You guys make me come to this forum again and again. Great job.

Regards,
*mavrick_101*
 
V

vandavn

Nếu là asp.net thì bạn làm như sau:
Ở đây chúng ta dùng component do hãng thứ 3 cung cấp nó hoàn toàn free. Äầu
tiên bạn download file: ICSharpCode.SharpZipLib.dll. Cái này search trên mạng
download vỠnha. Vì cái này làm biếng upload file lên wá nếu ai kiếm không ra
file này thì liên hệ với mình mình send cho.
Sau đó tạo 1 lớp unzip: và past đoạn code sau vào:

public void UnZipFiles(string zipPathAndFile, string outputFolder, string
password, bool deleteZipFile)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
if (password != null && password != String.Empty)
s.Password = password;
ZipEntry theEntry;
string tmpEntry = String.Empty;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = outputFolder;
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName != "")
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
if (theEntry.Name.IndexOf(".ini") < 0)
{
string fullPath = directoryName + "\" + theEntry.Name;
fullPath = fullPath.Replace("\ ", "\");
string fullDirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
FileStream streamWriter = File.Create(fullPath);
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();
if (deleteZipFile)
File.Delete(zipPathAndFile);
}


private static ArrayList GenerateFileList(string Dir)
{
ArrayList fils = new ArrayList();
bool Empty = true;
foreach (string file in Directory.GetFiles(Dir))
{
fils.Add(file);
Empty = false;
}

if (Empty)
{
if (Directory.GetDirectories(Dir).Length == 0)
{
fils.Add(Dir + @"/");
}
}

foreach (string dirs in Directory.GetDirectories(Dir))
{
foreach (object obj in GenerateFileList(dirs))
{
fils.Add(obj);
}
}
return fils;
}

vá»›i zipPathAndFile là file muốn giải nén >>> Cái này fải trỠđầy đủ dÆ°á»ng
dẫn nha ví dụ : path=Server.Mapth("/") >> Äây là thÆ° mục gốc. outputFolder là
đương dẫn muốn giải nén ra . Nếu đặt ở thư mục gốc thì outputFolder=path;
password file nén, và muốn xoá file zip cũ hay không thì xoá là true và không
xoá là false.

Còn muốn hàm Zip thì bạn cũng làm tương tự. Ngoài ra tthư viện này còn hổ
trợ file zip của linux luộn nha. File đuôi là tag đó...

Vì nhiá»u hàm dài wá nên không post thêm nếu ai muốn mã nguồn thì liên hệ
yahoo: (e-mail address removed), gmail : (e-mail address removed) .

Vậy là xong rùi :D :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top