分享SharpZipLip使用代码

zip类
public class ZipClass
    
{
        
/**//// <summary>
        
/// 压缩方法
        
/// </summary>
        
/// <param name="strPath">要压缩文件夹</param>
        
/// <param name="strFileName">生成的文件名</param>
        
/// <param name="PassWord">密码</param>
        
/// <returns>1 成功 -1输入的压缩文件夹为空 -2 输入的压缩文件夹目录不存在!</returns>

        public static int Zip(string strPath, string strFileName, string PassWord)
        
{
            
if (!String.IsNullOrEmpty(strPath))
            
{
                
//throw new Exception("输入的压缩文件夹为空");
                return -1;
            }

            
if (!Directory.Exists(strPath))
            
{
                
//throw new Exception("输入的压缩文件夹目录不存在!");
                return -2;
            }

            
if (!File.Exists(strFileName))
            
{
                File.Create(strFileName);
            }

            ZipOutputStream zip 
= new ZipOutputStream(File.Create(strFileName));
            
if (!string.IsNullOrEmpty(PassWord))
                zip.Password 
= PassWord;
            IList
<FileInfo> list = new List<FileInfo>();
            GetFileList(strPath, list);
            
try
            
{
                
byte[] buffer = new byte[2048];
                
int count = 0;
                
foreach (FileInfo fi in list)
                
{
                    count 
= 0;
                    
using (FileStream fs = File.OpenRead(fi.FullName))
                    
{
                        ZipEntry entry 
= new ZipEntry(fi.FullName.Substring(strPath.Length + 1));
                        entry.DateTime 
= fi.LastWriteTime;
                        entry.Size 
= fs.Length;
                        zip.PutNextEntry(entry);
                        
while ((count = fs.Read(buffer, 02048)) > 0)
                        
{
                            zip.Write(buffer, 
0, count);
                        }

                    }

                }

            }

            
catch
            
{
                
throw;
            }

            
finally
            
{
                zip.Finish();
                zip.Close();
            }

            
return 1;
        }

        
private static void GetFileList(string strPath, IList<FileInfo> list)
        
{
            DirectoryInfo di 
= new DirectoryInfo(strPath);
            
foreach (DirectoryInfo di1 in di.GetDirectories())
            
{
                GetFileList(di1.FullName, list);
            }

            
foreach (FileInfo fi in di.GetFiles())
            
{
                list.Add(fi);
            }

        }

        
/**//// <summary>
        
/// 解压缩文件
        
/// </summary>
        
/// <param name="strFileName">压缩文件</param>
        
/// <param name="strPath">目标目录</param>
        
/// <param name="PassWord">密码</param>
        
/// <returns>1 成功 -1输入的压缩文件夹为空 -2 输入的解压缩文件夹目录不存在!-3 文件不存在</returns>

        public static int UnZip(string strFileName, string strPath, string PassWord)
        
{
            
if (!String.IsNullOrEmpty(strPath))
            
{
                
//throw new Exception("输入的压缩文件夹为空");
                return -1;
            }

            
if (!Directory.Exists(strPath))
            
{
                
//throw new Exception("输入的压缩文件夹目录不存在!");
                return -2;
            }

            
if (!File.Exists(strFileName))
            
{
                
//throw new Exception("文件:" + strFileName + "不存在!");
                return -3;
            }

            ZipEntry entry;
            ZipInputStream zis 
= null;
            
try
            
{
                zis 
= new ZipInputStream(File.Open(strFileName, FileMode.Open));
                
if (!string.IsNullOrEmpty(PassWord))
                    zis.Password 
= PassWord;
                
byte[] buffer = new byte[2048];
                
int count = 0;
                
while ((entry = zis.GetNextEntry()) != null)
                
{
                    CreateDirList(entry.Name, strPath);
                    
string strPath1 = strPath + "\\" + entry.Name;
                    
using (FileStream streamWriter = File.Create(strPath1))
                    
{
                        
while ((count = zis.Read(buffer, 02048)) > 0)
                        
{
                            streamWriter.Write(buffer, 
0, count);
                        }

                    }

                    File.SetLastWriteTime(strPath1, entry.DateTime);
                }

            }

            
catch
            
{
                
throw;
            }

            
finally
            
{
                
if (zis != null)
                    zis.Close();
            }

            
return 1;
        }

        
private static void CreateDirList(string filename, string basePath)
        
{
            
string dirName = basePath;
            
string[] dirlevelname = filename.Split('\\');
            
for (int i = 0; i < dirlevelname.Length - 1; i++)
            
{
                dirName 
+= "\\" + dirlevelname[i];
                
if (Directory.Exists(dirName))
                
{
                    
continue;
                }

                Directory.CreateDirectory(dirName);
            }

        }

    }
没有什么说的,直接上代码.

转载于:https://www.cnblogs.com/LifelongLearning/archive/2008/01/03/1025131.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407674.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue基础之表单控件绑定

你可以用 v-model 指令在表单控件元素上创建双向数据绑定。它会根据控件类型自动选取正确的方法来更新元素。尽管有些神奇&#xff0c;但 v-model 本质上不过是语法糖&#xff0c;它负责监听用户的输入事件以更新数据&#xff0c;并特别处理一些极端的例子 基础用法&#xff1…

Makefile函数使用

使用函数 在Makefile中可以使用函数来处理变量&#xff0c;从而让我们的命令或是规则更为的灵活和具有智能。make所支持的函数也不算很多&#xff0c;不过已经足够我们的操作了。函数调用后&#xff0c;函数的返回值可以当做变量来使用。 [编辑] 函数的调用语法 函数调用&am…

[LeetCode] Search in Rotated Sorted Array

二分 : 判断条件 当a[left] < a[mid]时&#xff0c;可以肯定a[left..mid]是升序的 循环有序 一共有以下两种情况 第一种 / / / /    /  /  条件&#xff1a; (A[mid] > A[low]) &#xff0c;low~mid 二分&#xff0c;mid~high 递归 第二种 /  / / /   / /  条件…

失败者的特征

1 狂妄自大型:自以为老子天下第一,认为自己的认识才是正确的,才是没有错误的,实际上这些人的知识大部分来自于道听途说或是妄自没有求证的猜测就下定论,并一棍子打死,还不允许其它人也相信.2 口是心非型:心里面还是相信有某种力量能够支配人生,甚至还会经常拿着彩票的轨迹图天天…

@keyframes中translate和scale混用问题

当你动画的这个节点用到translate定位居中时&#xff0c;再使用动画scale就会出现不居中的问题 这时需要把keyframes中translate写在scale的前面就解决了 keyframes bubble-in {0% {transform:translateX(-50%) scale(0);}100% {transform:translateX(-50%) scale(1);} }

导入 IMP

Oracle 的导入实用程序 (Import utility) 允许从数据库提取数据&#xff0c;并且将数据写入操作系统文件。 imp 使用的基本格式&#xff1a; imp[username[/password[service]]] &#xff0c;以下例举 imp 常用用法。 1. 获取帮助 imp helpy 2. 导入一个完整数据库 imp sy…

Vue基础之组件

什么是组件&#xff1f; 组件&#xff08;Component&#xff09;是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素&#xff0c;封装可重用的代码。在较高层面上&#xff0c;组件是自定义元素&#xff0c; Vue.js 的编译器为它添加特殊功能。在有些情况下&#xff0c;组件也…

sql 保留小数

select ROUND(12.555, 2) --12.560 select cast(12.5550 as decimal(10,2)) --12.56 转载于:https://www.cnblogs.com/kedarui/p/3791337.html

React开发(105):没有定义变量报错

const { pageIndex, pageSize, selectTabVal, sortModel } this.state;

常用的前端JavaScript方法封装(49种)

1、输入一个值&#xff0c;返回其数据类型 function type(para) { return Object.prototype.toString.call(para) } 2、数组去重 function unique1(arr) { return […new Set(arr)] } function unique2(arr) { var obj {}; return arr.filter(ele > { if (!obj[ele]) { ob…

发一个flash+PHP的简单上传代码

示例文件1。Flash8:uploader.as--------------------------------------------------------------------------------import flash.net.FileReference; //调用上传控件&#xff0c;这个是必须的&#xff0c;要传文件就要用这个控件var frT…

Oracle SQL Loader数据导入

1、SQL LOADER是ORACLE的数据加载工具&#xff0c;通常用来将操作系统文件迁移到ORACLE数据库中。SQL*LOADER是大型数据仓库选择使用的加载方法&#xff0c;因为它提供了最快速的途径&#xff08;DIRECT&#xff0c;PARALLEL&#xff09;。 它使用的命令为&#xff1a;在NT下&a…

反射获取类的几种方法

1 public class Demo {2 3 /**4 * 反射&#xff1a;加载类&#xff0c;获得类的字节码5 * param args6 * throws ClassNotFoundException 7 */8 public static void main(String[] args) throws ClassNotFoundException {9 10 //…

Webpack基础之四个核心介绍

入口(Entry)&#xff1a; webpack 将创建所有应用程序的依赖关系图表(dependency graph)。图表的起点被称之为入口起点(entry point)。入口起点告诉 webpack 从哪里开始&#xff0c;并遵循着依赖关系图表知道要打包什么。可以将您应用程序的入口起点认为是根上下文(contextual…

React开发(106):方法定义 不然弹出框报错

hideSureModal () > {this.setState({sortModel: false,});};

为什么防火墙透传不过去VLAN11?

今天遇到一个问题请大家帮分析一下!2950上有两个VLAN需要通过防火墙透传VLAN1和VLAN11现在的问题是:VLAN11不能通过防火墙透传!vlan1可以透传过去.转载于:https://blog.51cto.com/liuzhu/59913

有关Java 锁原理

锁 锁是用来锁东西的&#xff0c;让别人打不开也看不到&#xff01;在线程中&#xff0c;用这个“锁”隐喻来说明一个线程在“操作”一个目标&#xff08;如一个变量&#xff09;的时候&#xff0c;如果变量是被锁住的&#xff0c;那么其他线程就对这个目标既“操作”不了&…

不能以根用户身份运行 Google Chrome 浏览器

在fedora12中安装了chrome浏览器&#xff0c;但是一运行出现以下提示&#xff1a; 不能以根用户身份运行 Google Chrome 浏览器。请以普通用户身份启动“Google Chrome 浏览器”。要以根用户身份运行&#xff0c;您必须为个人资料信息的存储指定其他的“--user-data-dir”。 …