ASP.NET生成压缩文件(rar打包)1【转】

2024-05-07 13:18

本文主要是介绍ASP.NET生成压缩文件(rar打包)1【转】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 
// <summary>
/// 生成压缩文件
/// </summary>
/// <param name="strZipPath">生成的zip文件的路径</param>
/// <param name="strZipTopDirectoryPath">源文件的上级目录</param>
/// <param name="intZipLevel">T压缩等级</param>
/// <param name="strPassword">压缩包解压密码</param>
/// <param name="filesOrDirectoriesPaths">源文件路径</param>
/// <returns></returns>
private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
{
try
{
List<string> AllFilesPath = new List<string>();
if (filesOrDirectoriesPaths.Length > 0) // get all files path
{
for (int i = 0; i < filesOrDirectoriesPaths.Length; i++)
{
if (File.Exists(filesOrDirectoriesPaths[i]))
{
AllFilesPath.Add(filesOrDirectoriesPaths[i]);
}
else if (Directory.Exists(filesOrDirectoriesPaths[i]))
{
GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
}
}
}if (AllFilesPath.Count > 0)
{ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
zipOutputStream.SetLevel(intZipLevel);
zipOutputStream.Password = strPassword;for (int i = 0; i < AllFilesPath.Count; i++)
{
string strFile = AllFilesPath[i].ToString();
try
{
if (strFile.Substring(strFile.Length - 1) == "") //folder
{
string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring(1);
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
}
else //file
{
FileStream fs = File.OpenRead(strFile);byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring(0);
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(buffer, 0, buffer.Length);fs.Close();
fs.Dispose();
}
}
catch
{
continue;
}
}zipOutputStream.Finish();
zipOutputStream.Close();return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}/// <summary>
/// Gets the directory files.
/// </summary>
/// <param name="strParentDirectoryPath">源文件路径</param>
/// <param name="AllFilesPath">所有文件路径</param>
private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
{
string[] files = Directory.GetFiles(strParentDirectoryPath);
for (int i = 0; i < files.Length; i++)
{
AllFilesPath.Add(files[i]);
}
string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
for (int i = 0; i < directorys.Length; i++)
{
GetDirectoryFiles(directorys[i], AllFilesPath);
}
if (files.Length == 0 && directorys.Length == 0) //empty folder
{
AllFilesPath.Add(strParentDirectoryPath);
}
}调用string strZipPath = @"D:\ConsultSystem\mgr\user.zip";
string strZipTopDirectoryPath = @"D:\ConsultSystem\mgr\";
int intZipLevel = 6;
string strPassword = "";
string[] filesOrDirectoriesPaths = new string[] { @"D:\ConsultSystem\mgr\user.txt" };


这篇关于ASP.NET生成压缩文件(rar打包)1【转】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/967460

相关文章

android 带与不带logo的二维码生成

该代码基于ZXing项目,这个网上能下载得到。 定义的控件以及属性: public static final int SCAN_CODE = 1;private ImageView iv;private EditText et;private Button qr_btn,add_logo;private Bitmap logo,bitmap,bmp; //logo图标private st

Windows中,.net framework 3.5安装

安装.net framework,目前已知2种方法,如下: 一、在MSDN下载对应的安装包,安装,这种可能无法安装成功,概率很大,不成功使用第二种方法,基本上没问题。 二、win8/8.1/10 下安装 .net framework 3.5.1: 1. 打开 win8/8.1/10 安装盘(这里指系统安装镜像文件),提取 sources\sxs 文件夹到 X:\sources\sxs (X代

FastAdmin/bootstrapTable 表格中生成的按钮设置成文字

公司有个系统后台框架用的是FastAdmin,后台表格的操作栏按钮只有图标,想要设置成文字。 查资料后发现其实很简单,主需要新增“text”属性即可,如下 buttons: [{name: 'acceptcompany',title: '复核企业',text:'复核企业',classname: 'btn btn-xs btn-primary btn-dialog',icon: 'fa fa-pe

ASP.Net.WebAPI和工具PostMan

1.WebAPI概述 1.1 WebAPI WebAPI 是一种传统的方式,用于构建和暴露 RESTUI风格的Web服务。它提供了丰富的功能和灵活性,可以处理各种HTTP请求,并支持各种数据格式,如JSON、XML等。 WebAPI使用控制器(Controllers)和动作方法(ActionMethods)的概念、通过路由配置将请求映射到相应的方法上。 开发人员可以使用各种属性和过滤器来处

打包体积分析和优化

webpack分析工具:webpack-bundle-analyzer 1. 通过<script src="./vue.js"></script>方式引入vue、vuex、vue-router等包(CDN) // webpack.config.jsif(process.env.NODE_ENV==='production') {module.exports = {devtool: 'none

AndroidStudio打包处理

AndroidStudio非常强大,公司最近有一个需求是要实现对一个APP进行多个版本的打包,而且可以同时安装在手机上。这个需求详细一点的描述是:公司有一个APP,有多个开发商要使用我们的APP,为了大家都想有一个自己的APP,而且图标不一样,app名字不一样,背景不一样等。我查询了一下资料发现,在AndroidStudio的gradle是可以配置的。在此特意写一篇文章记录分享。 配置签名 首

【ASP.NET】 No 'Access-Control-Allow-Origin' header is present on the requested resource.

前端JS用XMLHttpRequest,请求后端数据。出现了No ‘Access-Control-Allow-Origin’ header is present on the requested resource. 我是使用的ASP.NET框架。 解决办法: 在Web.config文件相应地方添加: <?xml version="1.0" encoding="utf-8"?><confi

Tkinter和selenium结合实现登录UC后台,最后打包成exe

主要实现的功能:小号模式自动登录UC阿里汇川广告后台,屏蔽账号密码输入 主要用的技术:用Tkinter展示所有的广告账号界面,使用selenium控制谷歌浏览器,打开阿里汇川登录页,登录汇川后台。 第一次写,遇到的坑比较多,三天,搞定。给自己一个棒棒~☺️ import Tkinter as tk import osimport sysimport requestsfrom sel

PHP生成csv格式Excel,秒级别实现excel导出功能

防止报超内存,兼容中文,兼容科学技术法。 爽。。。。很爽。。。。 /*** 告诉浏览器下载csv文件* @param string $filename*/public static function downloadCsv($data, $filename, $encoding = 'utf-8'){header("Content-type: text/csv");header("Conten

PHP 读取或生成大的Excel

场景,在很多情况下,需要读取Excel文件。 常用的有PHPExcel包或者使用 maatwebsite/excel 包 但是使用这个包读取或生成excel,如果excel文件过大,很容易出现超内存情况。 解决方法: 上传:要求上传者使用.csv 文件上传。然后使用php自带的 fgetcsv()函数来读取文件。http://php.net/manual/zh/function.fgetc