这篇文章主要为大家详细介绍了C#文件帮助类(解压,压缩)的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣C#文件帮助类(解压,压缩)的简单示例的小伙伴,下面一起跟随四海网的小编罗X来看看吧。<br>
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Share
{
public class FileHelp
{
#region 删除指定路径的文件
/// <summary>
/// 删除指定路径的文件
/// </summary>
/// <param name="path"></param>
public static void DeleteFile(string path)
{
if (File.Exists(path))//判断文件是不是存在
{
File.Delete(path);//如果存在则删除
}
}
/// <summary>
/// 根据路径删除文件或者文件夹
/// </summary>
/// <param name="path"></param>
public static void DeleteFile2(string path)
{
if (!Directory.Exists(path))//如果不存在,则返回
{
return;
}
FileAttributes attr = File.GetAttributes(path);
if (attr == FileAttributes.Directory)
{
Directory.Delete(path, true);
}
else
{
File.Delete(path);
}
}
#endregion
#region 压缩文件
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="filesPath">文件路径</param>
/// <param name="zipFilePath">压缩路径</param>
private static void CreateZipFile(string filesPath, string zipFilePath)
{
if (!Directory.Exists(filesPath))
{
Console.WriteLine("Cannot find directory '{0}'", filesPath);
return;
}
try
{
string[] filenames = Directory.GetFiles(filesPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9); // 压缩级别 0-9
//s.Password = "123"; //Zip压缩文件密码
byte[] buffer = new byte[4096]; //缓冲区大小
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
}
#endregion
#region 解压
/// <summary>
/// 解压,解压到exe目录下
/// </summary>
/// <param name="zipFilePath">要解压的文件路径</param>
public static bool UnZipFile(string zipFilePath, Action<int> proValue,int pos, int posMax)
{
if (!File.Exists(zipFilePath))
{
Console.WriteLine("Cannot find file '{0}'", zipFilePath);
return false;
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
proValue(pos + 1 >= posMax ? pos : pos++);
Console.WriteLine(theEntry.Name);
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName.Length > 0)
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(theEntry.Name))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
return true;
}
//使用GZIP解压文件的方法
/// <summary>
/// 使用GZIP解压文件的方法
/// </summary>
/// <param name="zipfilename"></param>
/// <param name="unzipfilename"></param>
/// <returns></returns>
static bool UnGzipFile(string zipfilename, string unzipfilename)
{
bool blResult;//表示解压是否成功的返回结果
//创建压缩文件的输入流实例
using (GZipInputStream zipFile = new GZipInputStream(File.OpenRead(zipfilename)))
{
//创建目标文件的流
FileStream destFile = File.Open(unzipfilename, FileMode.Create);
try
{
int buffersize = 2048;//缓冲区的尺寸,一般是2048的倍数
byte[] FileData = new byte[buffersize];//创建缓冲数据
while (buffersize > 0)//一直读取到文件末尾
{
buffersize = zipFile.Read(FileData, 0, buffersize);//读取压缩文件数据
destFile.Write(FileData, 0, buffersize);//写入目标文件
}
blResult = true;
}
catch (Exception ee)
{
//Console.WriteLine(ee.Message);
blResult = false;
}
destFile.Close();//关闭目标文件
zipFile.Close();//关闭压缩文件
}
return blResult;
}
#endregion
#region 根据文件路径获取string
public static string GetFileJson(string filepath)
{
string json = string.Empty;
using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
{
json = sr.ReadToEnd();
}
}
return json;
}
#endregion
}// www.q1010.com
}
本文来自:http://www.q1010.com/194/16381-0.html
注:关于C#文件帮助类(解压,压缩)的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:类
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。