您的位置:首页 > 编程语言 > ASP

asp.net 文件打包成压缩包

2016-03-23 15:56 423 查看
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.Text = DateTime.Now.ToLocalTime().ToString();
}
/// <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) == "") //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);
}
}

private void timer1_Tick(object sender, EventArgs e)
{
this.Text = DateTime.Now.ToLocalTime().ToString();
string date = ConfigurationManager.AppSettings["daochu"];

string date2 = DateTime.Now.ToLongDateString().ToString();

if (DateTime.Parse(this.Text) == DateTime.Parse(date2 + " " + date))
{
string wname1 = System.DateTime.Now.ToString("yyyyMMdd");
string lj = @"D:\ebayzip\";
string strZipPath = lj + wname1 + ".zip";
if (!Directory.Exists(lj))
{
Directory.CreateDirectory(lj);
//cl_initPage.v_DeBugLog("path创建成功:" + path);
}
else
{
//cl_initPage.v_DeBugLog("path创建失败:" + path);
}
string strZipTopDirectoryPath = @"D:\EbayWishProj\";
int intZipLevel = 6;
string strPassword = "";

string[] filesOrDirectoriesPaths = new string[] { @"D:\EbayWishProj\" + wname1 };
bool a = Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: