互联网大文件上传
Posted 松鼠的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了互联网大文件上传相关的知识,希望对你有一定的参考价值。
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现。
下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压。
ASP.NET页面设计:TextBox和Button按钮。
TextBox中需要自己受到输入文件夹的路径(包含文件夹),通过Button实现选择文件夹的问题还没有解决,暂时只能手动输入。
两种方法:生成rar和zip。
1.生成rar
using Microsoft.Win32;
using System.Diagnostics;
protected void Button1Click(object sender, EventArgs e)
{
RAR(@"E:\\95413594531\\GIS", "tmptest", @"E:\\95413594531\\");
}
///
/// 压缩文件
///
/// 需要压缩的文件夹或者单个文件
/// 生成压缩文件的文件名
/// 生成压缩文件保存路径
///
protected bool RAR(string DFilePath, string DRARName,string DRARPath)
{
String therar;
RegistryKey theReg;
Object theObj;
String theInfo;
ProcessStartInfo theStartInfo;
Process theProcess;
try
{
theReg = Registry.ClassesRoot.OpenSubKey(@"Applications\\WinRAR.exe\\Shell\\Open\\Command"); //注:未在注册表的根路径找到此路径
theObj = theReg.GetValue("");
therar = theObj.ToString();
theReg.Close();
therar = therar.Substring(1, therar.Length - 7);
theInfo = " a " + " " + DRARName + " " + DFilePath +" -ep1"; //命令 + 压缩后文件名 + 被压缩的文件或者路径
theStartInfo = new ProcessStartInfo();
theStartInfo.FileName = therar;
theStartInfo.Arguments = theInfo;
theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
theStartInfo.WorkingDirectory = DRARPath ; //RaR文件的存放目录。
theProcess = new Process();
theProcess.StartInfo = theStartInfo;
theProcess.Start();
theProcess.WaitForExit();
theProcess.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
///
/// 解压缩到指定文件夹
///
/// 压缩文件存在的目录
/// 压缩文件名称
/// 解压到文件夹
///
protected bool UnRAR(string RARFilePath,string RARFileName,string UnRARFilePath)
{
//解压缩
String therar;
RegistryKey theReg;
Object theObj;
String theInfo;
ProcessStartInfo theStartInfo;
Process theProcess;
try
{
theReg = Registry.ClassesRoot.OpenSubKey(@"Applications\\WinRar.exe\\Shell\\Open\\Command");
theObj = theReg.GetValue("");
therar = theObj.ToString();
theReg.Close();
therar = therar.Substring(1, therar.Length - 7);
theInfo = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;
theStartInfo = new ProcessStartInfo();
theStartInfo.FileName = therar;
theStartInfo.Arguments = theInfo;
theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
theProcess = new Process();
theProcess.StartInfo = theStartInfo;
theProcess.Start();
return true;
}
catch (Exception ex)
{
return false;
}
}
注:这种方法在在电脑注册表中未找到应有的路径,未实现,仅供参考。
2.生成zip
通过调用类库ICSharpCode.SharpZipLib.dll
该类库可以从网上下载。也可以从本链接下载:SharpZipLib_0860_Bin.zip
增加两个类:Zip.cs和UnZip.cs
(1)Zip.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Collections;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
namespace UpLoad
{
/// <summary>
/// 功能:压缩文件
/// creator chaodongwang 2009-11-11
/// </summary>
public class Zip
{
/// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="FileToZip">被压缩的文件名称(包含文件路径)</param>
/// <param name="ZipedFile">压缩后的文件名称(包含文件路径)</param>
/// <param name="CompressionLevel">压缩率0(无压缩)-9(压缩率最高)</param>
/// <param name="BlockSize">缓存大小</param>
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel)
{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("文件:" + FileToZip + "没有找到!");
}
if (ZipedFile == string.Empty)
{
ZipedFile = Path.GetFileNameWithoutExtension(FileToZip) + ".zip";
}
if (Path.GetExtension(ZipedFile) != ".zip")
{
ZipedFile = ZipedFile + ".zip";
}
////如果指定位置目录不存在,创建该目录
//string zipedDir = ZipedFile.Substring(0,ZipedFile.LastIndexOf("\\\\"));
//if (!Directory.Exists(zipedDir))
// Directory.CreateDirectory(zipedDir);
//被压缩文件名称
string filename = FileToZip.Substring(FileToZip.LastIndexOf(\'\\\\\') + 1);
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry(filename);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[2048];
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)
内网大文件传输断点续传源码