在没有第三方controll的情况下在asp.net c#中压缩文件夹 [英] zip a folder in asp.net c# without third party controll

查看:82
本文介绍了在没有第三方controll的情况下在asp.net c#中压缩文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有第三方控制的asp.net c#中压缩文件夹

zip a folder in asp.net c# without third party controll

推荐答案


添加引用...

Add reference...

using java.util;
using java.util.zip;
using java.io;

public string ZipFile(string savePath, string[] attachFiles)
    {
        string sFlag = "Error";
        try
        {
            FileOutputStream fileOutput = new FileOutputStream(savePath);
            ZipOutputStream zipOutput = new ZipOutputStream(fileOutput);
            FileInputStream fileInput = null;
            foreach (string file in attachFiles)
            {
                fileInput = new FileInputStream(file);
                ZipEntry zipEntry = new ZipEntry(Path.GetFileName(file));
                zipOutput.putNextEntry(zipEntry);
                sbyte[] buffer = new sbyte[1024];
                int len = 0;
                while ((len = fileInput.read(buffer)) >= 0)
                {
                    zipOutput.write(buffer, 0, len);
                }
            }
            zipOutput.closeEntry();
            fileInput.close();
            zipOutput.close();
            fileOutput.close();
            return sFlag = "Success";
        }
        catch (Exception)
        {
            return sFlag;
        }
    }
    private List<ZipEntry> GetZipFiles(ZipFile zipfill)
    {
        List<ZipEntry> listZip = new List<ZipEntry>();
        Enumeration zipEnum = zipfill.entries();
        while (zipEnum.hasMoreElements())
        {
            ZipEntry zipEntry = (ZipEntry)zipEnum.nextElement();
            listZip.Add(zipEntry);
        }
        return listZip;
    }
    public string UnZipFile(string zippedFile, string destPath)
    {
        string sFlag = "Error";
        try
        {
            ZipFile zipfile = new ZipFile(zippedFile);
            List<ZipEntry> zipFiles = GetZipFiles(zipfile);
            foreach (ZipEntry zipFile in zipFiles)
            {
                if (!zipFile.isDirectory())
                {
                    InputStream s = zipfile.getInputStream(zipFile);
                    try
                    {
                        Directory.CreateDirectory(destPath + "\\" + Path.GetDirectoryName(zipFile.getName()));
                        FileOutputStream dest = new FileOutputStream(Path.Combine(destPath + "\\" + Path.GetDirectoryName(zipFile.getName()), Path.GetFileName(zipFile.getName())));
                        try
                        {
                            int len = 0;
                            sbyte[] buffer = new sbyte[7168];
                            while ((len = s.read(buffer)) >= 0)
                            {
                                dest.write(buffer, 0, len);
                            }
                        }
                        finally
                        {
                            dest.close();
                        }
                    }
                    finally
                    {
                        s.close();
                    }
                }
                sFlag = "Success";
            }
            return sFlag;
        }
        catch (Exception)
        {
            return sFlag;
        }
    }


CP本身有一篇文章.

看看
There is an Article at CP itself.

Have a look


这篇关于在没有第三方controll的情况下在asp.net c#中压缩文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆