using System;
using System.Web;
using System.Text;
using
System.Runtime.InteropServices;
using System.IO;
using
System.IO.Compression;
namespace Pub.Class
{
///
<summary>
/// 文件目录操作类
/// </summary>
public
class FileFolder
{
#region CreateFolder/CreateDir
/// <summary>
/// 目录不存在时新建
///
</summary>
/// <param
name="filePath">目录(相对路径)</param>
public static void
CreateFolder(string filePath)
{
string[] PathArr =
filePath.Split(new string[] { "/" }, StringSplitOptions.None);
string _path = PathArr[0];
for (int i = 1; i < PathArr.Length;
i++) {
_path = _path + "/" + PathArr[i];
string _filePath = HttpContext.Current.Server.MapPath(_path);
if (!System.IO.Directory.Exists(_filePath)) {
System.IO.Directory.CreateDirectory(_filePath);
}
}
}
/// <summary>
/// 创建目录
///
</summary>
/// <param name="name">名称</param>
///
<returns>创建是否成功</returns>
[DllImport("dbgHelp",
SetLastError=true)]
private static extern bool
MakeSureDirectoryPathExists(string name);
/// <summary>
/// 建立文件夹
/// </summary>
/// <param
name="name"></param>
/// <returns></returns>
public static bool CreateDir(string name)
{
return
MakeSureDirectoryPathExists(name);
}
///
<summary>
/// 目录是否存在
/// </summary>
/// <param name="folderPath"></param>
///
<returns></returns>
public static bool
FolderExists(string folderPath) {
return
System.IO.Directory.Exists(folderPath);
}
#endregion
#region
FileExists/DelAllFile/DelFile/GetAllFile/BackupFile
///
<summary>
/// 返回文件是否存在
/// </summary>
/// <param
name="filename">文件名</param>
///
<returns>是否存在</returns>
public static bool FileExists(string
filename)
{
return System.IO.File.Exists(filename);
}
/// <summary>
/// 删除指定目录下的所有文件
///
</summary>
/// <param
name="path"></param>
public static void DelAllFile(string
path) {
DirectoryInfo Folder = new DirectoryInfo(path);
FileInfo[] subFiles = Folder.GetFiles();
for (int j = 0; j <
subFiles.Length; j++) {
subFiles[j].Delete();
}
}
/// <summary>
/// 删除指定文件
/// </summary>
/// <param
name="filePath"></param>
public static void DelFile(string
filePath) {
if (FileExists(filePath))
System.IO.File.Delete(filePath);
}
///
<summary>
/// 取指定目录下的所有文件名
///
</summary>
/// <param
name="path"></param>
///
<returns></returns>
public static StringBuilder
GetAllFile(string path) {
StringBuilder fileList = new
StringBuilder();
try {
DirectoryInfo Folder =
new DirectoryInfo(path);
FileInfo[] subFiles =
Folder.GetFiles();
for (int j = 0; j < subFiles.Length;
j++) {
fileList.Append(subFiles[j].Name +
"|");
}
} catch { }
return
fileList.Length.Equals(0) ? fileList : fileList.Remove(fileList.Length - 1,
1);
}
/// <summary>
///
取目录下的所有文件
/// </summary>
/// <param
name="path"></param>
/// <param
name="fileList"></param>
/// <param
name="delStr"></param>
public static void GetAllFile(string
path,ref StringBuilder fileList, string delStr) {
if
(delStr.Equals("")) delStr = path;
delStr =
delStr.Substring(delStr.Length - 1).Equals("\\") ? delStr : delStr +
"\\";
string[] files =
System.IO.Directory.GetFiles(path);
foreach (string s in files)
fileList.Append(s + "|").Replace(delStr,"");
string[] dir =
System.IO.Directory.GetDirectories(path);
foreach (string s in
dir) GetAllFile(s, ref fileList,delStr);
}
///
<summary>
/// 备份文件
/// </summary>
/// <param name="sourceFileName"></param>
/// <param
name="destFileName"></param>
/// <param
name="overwrite"></param>
///
<returns></returns>
public static bool BackupFile(string
sourceFileName, string destFileName, bool overwrite) {
if
(!File.Exists(sourceFileName)) return false;
if (!overwrite
&& File.Exists(destFileName)) return false;
try
{
File.Copy(sourceFileName, destFileName,
true);
} catch {
return false;
}
return true;
}
#endregion
///
<summary>
/// 取文件编码
/// </summary>
/// <param name="fileName"></param>
///
<returns></returns>
public static Encoding
GetFileEncoding(string fileName) {
/*byte[] Unicode=new
byte[]{0xFF,0xFE};
byte[] UnicodeBIG=new
byte[]{0xFE,0xFF};
byte[] UTF8=new
byte[]{0xEF,0xBB,0xBF};*/
try {
FileStream
fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
BinaryReader r = new
BinaryReader(fs,System.Text.Encoding.Default);
byte[] ss =
r.ReadBytes(3);
r.Close();
fs.Close();
fs.Dispose();
if(ss[0]>=0xEF) {
if (ss[0]==0xEF &&
ss[1]==0xBB && ss[2]==0xBF)
return
System.Text.Encoding.UTF8;
else if (ss[0]==0xFE
&& ss[1]==0xFF)
return
System.Text.Encoding.BigEndianUnicode;
else if
(ss[0]==0xFF && ss[1]==0xFE)
return
System.Text.Encoding.Unicode;
else
return
System.Text.Encoding.Default;
} else return
System.Text.Encoding.Default;
} catch {
return
System.Text.Encoding.Default;
}
}
public static void GZipCompress(string inFilename, string outFilename)
{
FileStream sourceFile =
File.OpenRead(inFilename);
FileStream destFile =
File.Create(outFilename);
GZipStream compStream = new
GZipStream(destFile, CompressionMode.Compress);
int theByte =
sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
sourceFile.Close();
compStream.Close();
destFile.Close();
}
public static void
GZipDecompress(string inFileName, string outFileName) {
FileStream sourceFile = File.OpenRead(inFileName);
FileStream
destFile = File.Create(outFileName);
GZipStream compStream = new
GZipStream(sourceFile, CompressionMode.Decompress);
int theByte =
compStream.ReadByte();
while (theByte != -1)
{
destFile.WriteByte((byte)theByte);
theByte = compStream.ReadByte();
}
destFile.Close();
compStream.Close();
sourceFile.Close();
}
public static long
GetDirectorySize(string path) {
long size = 0;
string[] files = System.IO.Directory.GetFiles(path);
foreach
(string s in files) size += new FileInfo(s).Length;
string[] dir
= System.IO.Directory.GetDirectories(path);
foreach (string s in
dir) size += GetDirectorySize(s);
return size;
}
public static long GetFileSize(string path) {
if
(!FileExists(path)) return 0;
return new FileInfo(path).Length;
}
}
}