| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1218 人关注过本帖, 1 人收藏
标题:谁用ICSharpCode制作过压缩解压文件夹的程序,帮忙看看
只看楼主 加入收藏
tomtory
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:705
专家分:0
注 册:2008-6-12
收藏(1)
 问题点数:0 回复次数:1 
谁用ICSharpCode制作过压缩解压文件夹的程序,帮忙看看
我用ICSharpCode制作的压缩解压程序  现在压缩没有问题 可以解压出线了问题
在winform中又正常
附上源码看看:
下面是压缩的程序
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;

using

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

/// <summary>
/// ZipFloClass 的摘要说明
/// </summary>
public class ZipFloClass
{
    public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
    {
        //如果文件没有找到,则报错
        if (!(FileToZip))
        {
            throw new ("指定要压缩的文件: " + FileToZip + " 不存在!");
        }

         StreamToZip = new (FileToZip, , );
         ZipFile = (ZipedFile);
        ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
        ZipEntry ZipEntry = new ZipEntry("ZippedFile");
        ZipStream.PutNextEntry(ZipEntry);
        ZipStream.SetLevel(CompressionLevel);
        byte[] buffer = new byte[BlockSize];
        System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
        ZipStream.Write(buffer, 0, size);
        try
        {
            while (size < StreamToZip.Length)
            {
                int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                ZipStream.Write(buffer, 0, sizeRead);
                size += sizeRead;
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        ZipStream.Finish();
        ZipStream.Close();
        StreamToZip.Close();
    }

    public static string filePath = "";
    public static string GetContrast(string path)
    {
        string[] objFiles = Directory.GetFiles(path);
        for (int i = 0; i < objFiles.Length; i++)
        {
            if (filePath.Equals(""))
                filePath = objFiles[i];
            else
                filePath += "<IS1>" + objFiles[i];
        }
        string[] objDorectories = Directory.GetDirectories(path);
        for (int i = 0; i < objDorectories.Length; i++)
        {
            GetContrast(objDorectories[i]);
        }
        return filePath;
    }

    /// <summary>
    /// 压缩目录
    /// </summary>
    /// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)</param>
    public static void ZipFileDictory(string[] args)
    {
        string[] filenames = GetContrast(args[0]).Split(new string[] { "<IS1>" }, StringSplitOptions.None);

        Crc32 crc = new Crc32();
        ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
        s.SetLevel(6);

        foreach (string file in filenames)
        {
            //打开压缩文件
            FileStream fs = File.OpenRead(file);

            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            ZipEntry entry = new ZipEntry(file);

            entry.DateTime = DateTime.Now;

            entry.Size = fs.Length;
            fs.Close();

            crc.Reset();
            crc.Update(buffer);

            entry.Crc = crc.Value;

            s.PutNextEntry(entry);

            s.Write(buffer, 0, buffer.Length);

        }

        s.Finish();
        s.Close();
    }

    /// <summary>
    /// 压缩文件
    /// </summary>
    /// <param name="FileToZip">要进行压缩的文件名</param>
    /// <param name="ZipedFile">压缩后生成的压缩文件名</param>
    public static void ZipFile(string FileToZip, string ZipedFile)
    {
        //如果文件没有找到,则报错
        if (!File.Exists(FileToZip))
        {
            throw new ("指定要压缩的文件: " + FileToZip + " 不存在!");
        }
        FileStream fs = File.OpenRead(FileToZip);
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        fs.Close();

        FileStream ZipFile = File.Create(ZipedFile);
        ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
        ZipEntry ZipEntry = new ZipEntry("ZippedFile");
        ZipStream.PutNextEntry(ZipEntry);
        ZipStream.SetLevel(6);

        ZipStream.Write(buffer, 0, buffer.Length);
        ZipStream.Finish();
        ZipStream.Close();
    }
}


下面是解压的程序:
using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.GZip;


/// <summary>
/// UnZipFloClass 的摘要说明
/// </summary>
public class UnZipFloClass
{

    /// <summary>
    /// 解压功能(解压压缩文件到指定目录)
    /// </summary>
    /// <param name="args">待解压的文件</param>
    /// <param name="args">指定解压目标目录</param>
    public static void UnZip(string[] args)
    {
        ZipInputStream s = new ZipInputStream(File.OpenRead(@args[0].Trim()));
        ZipEntry theEntry;

        //if (!Directory.Exists(@args[1].Trim()))
        //{
        //    Directory.CreateDirectory(@args[1].Trim());
        //}
        string path = @args[1].Trim();
        string rootDir = " ";
        string rootFile = " ";
        while ((theEntry = s.GetNextEntry()) != null)
        {
            //string directoryName = Path.GetDirectoryName(theEntry.Name);
            rootDir = Path.GetDirectoryName(theEntry.Name);

            //得到根目录下的第一级子文件夹的名称
            if (rootDir.IndexOf("\\") >= 0)
            {
                rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
            }

            string dir = Path.GetDirectoryName(theEntry.Name);
            //根目录下的第一级子文件夹的下的文件夹的名称
            string fileName = Path.GetFileName(theEntry.Name);

            //根目录下的文件名称
            if (dir != " ")
            //创建根目录下的子文件夹,不限制级别
            {
                if (!Directory.Exists(@args[1].Trim() + dir))
                {
                    path = @args[1].Trim() + dir;
                    //在指定的路径创建文件夹
                    Directory.CreateDirectory(path);
                }
            }
            else if (dir == " " && fileName != "")
            //根目录下的文件
            {
                path = @args[1].Trim();
                rootFile = fileName;
            }
            else if (dir != " " && fileName != "")
            //根目录下的第一级子文件夹下的文件
            {
                if (dir.IndexOf("\\") > 0)
                //指定文件保存的路径
                {
                    path = @args[1].Trim() + dir;
                }
            }

            if (dir == rootDir)
            //判断是不是需要保存在根目录下的文件
            {
                path = @args[1].Trim() + rootDir;
            }

            //Directory.CreateDirectory(directoryName);
            if (fileName != String.Empty)
            {
                FileStream streamWriter = File.Create(path + "\\" + fileName);

                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();
    }
}

在解压的时候读入文件数据流时出错 :
size = s.Read(data, 0, data.Length);  就是这句  

还有while ((theEntry = s.GetNextEntry()) != null)这句在循环第二次也出错

调用方法是   
压缩:
string[] FilePathS = new string[2];
            FilePathS[0] = "需要压缩目录";
            FilePathS[1] = "压缩后存放地址加上压缩文件名字";
            ZipFloClass.ZipFileDictory(FilePathS);

调用解压 :
            string[] FilePathS = new string[2];
            FilePathS[0] = "需要解压文件";
            FilePathS[1] = "文件解压存放地址";
            UnZipFloClass.UnZip(FilePathS);

SharpZipLib.dll在附件中
程序为web程序

ICSharpCode.SharpZipLib.rar (69.69 KB)
搜索更多相关主题的帖子: ICSharpCode 文件 压缩 制作 
2008-08-21 11:15
jaychang
Rank: 1
等 级:禁止访问
帖 子:148
专家分:0
注 册:2007-5-3
收藏
得分:0 
我跟你一样也出现了这个问题...不知道是为什么

http://bbs.    编程GIS论坛
交流软件开发技术与GIS技术
2008-08-21 13:33
快速回复:谁用ICSharpCode制作过压缩解压文件夹的程序,帮忙看看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031438 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved