ASP. Net file compression

Source: Internet
Author: User
Tags getstream httpcontext

Using System;
Using System.Web;
Using System.IO;
Using System.Configuration;
Using System.IO.Packaging;
Using System.ComponentModel;
Using System.Net;

<summary>
File compression
</summary>
public class Ziphelper
{

private static readonly string FileUrl = configurationmanager.appsettings["FileUrl"];
private static readonly string applicationpath = configurationmanager.appsettings["Applicationpath"];
<summary>
File address
</summary>
private string Zipfolderpath {get; set;}
<summary>
Compression level
</summary>
Public compressionoption comoption {get; set;}
<summary>
Resource lock Tag
</summary>
private static Object locker = new Object ();
private string zipdefname = String. Empty;
private int currentnum = 0;

<summary>
Zip
</summary>
<param name= "Zipfolderpath" > Store zip file address </param>
<param name= "Comoption" > Compression level </param>
Public Ziphelper (String zipfolderpath, CompressionOption comoption)
{
Zipdefname = Guid.NewGuid (). ToString (). Replace ("-", "") + ". zip";
This.comoption = comoption;
if (!string. IsNullOrEmpty (Zipfolderpath))
{
This.zipfolderpath = Zipfolderpath;
}
Else
{
This.zipfolderpath = HttpContext.Current.Server.MapPath (string. Format ("{0}/zipfoldercache/{1}", Applicationpath, Zipdefname));
}
}

<summary>
Compress files
</summary>
<param name= "urllists" > File relative path url</param>
<param name= "MSG" > Messages </param>
<param name= "Urltype" ></param>
<returns></returns>
public bool Zipfolderlist (string urllists, ref string msg, Urltypeenum Urltype = urltypeenum.local)
{
if (string. IsNullOrEmpty (urllists))
{
msg = "URL address error";
return false;
}
String filePath = zipfolderpath.substring (0, zipfolderpath.lastindexof (' \ \ ') + 1);
if (! Directory.Exists (FilePath))///folder tree exists, re-create file directory if not present
{
Directory.CreateDirectory (FilePath);
}
string[] Urlarr = Urllists.trim (';'). Split (';');
using (Package package = Package.Open (Zipfolderpath, FileMode.OpenOrCreate))
{
foreach (string URL in Urlarr)
{
Try
{
Zipurlfolder (URL, package, urltype);
}
catch (InvalidOperationException e)
{
continue;//files are the same, ignoring
}
catch (Exception ex)
{
msg = "Packaging error!";
return false;
}
}
}
msg = string. Format ("{0}/zipfoldercache/{1}", FileUrl, Zipdefname);
return true;
}

//<summary>
//zip file
//</summary>
//<param name= "url" > File relative address </param>
<param name= "Package" > Compression pack </param>
//<param name= "Urltype" ></PARAM>
private void Zipurlfolder (string URL, package package, urltypeenum Urltype = urltypeenum.local)
{
String zipnewfilepath = Url.s Ubstring (URL. LastIndexOf ("/"));
PackagePart part = package. CreatePart (New Uri (Zipnewfilepath, urikind.relative), System.Net.Mime.MediaTypeNames.Application.Zip, comoption);
if (Urltype = = urltypeenum.local)
{
FileInfo fi = new FileInfo (HttpContext.Current.Server.MapPath ( Applicationpath + "/" + URL));
using (FileStream fs = fi. OpenRead ())
{
CopyStream (FS, part. GetStream ());
}
}
Else if (Urltype = = Urltypeenum.online)
{
using (Stream stream = webrequest.create (URL). GetResponse (). GetResponseStream ())
{
CopyStream (stream, part. GetStream ());
}
}
}

//<summary>
///Stream copy
//</summary>
//<param name= "source" > Origin </param>
// <param name= "target" > Destination stream </param>
private void CopyStream (stream source, stream target)
{
MemoryStream ms = new MemoryStream (); Prevents {System.Net.ConnectStream} from reading bytes that are not full
source. CopyTo (MS); Write memory stream
Byte[] b = new Byte[ms. Length];
Ms. Seek (0, Seekorigin.begin);
Const int buffersize = 0x1000;
byte[] buffer = new Byte[buffersize];
int readindex = 0;
while (Readindex = Ms. Read (buffer, 0, buffersize)) > 0)
{
target. Write (buffer, 0, buffersize);
}
Ms. Dispose ();
Ms. Close ();
}

<summary>
Network type
</summary>
public enum Urltypeenum
{
<summary>
Network URL
</summary>
[Description ("Network URL")]
Online = 0,
<summary>
Local Service URL
</summary>
[Description ("Local Service URL")]
Local = 1
}
}

ASP. Net file compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.