C#要做完成查询某个目录下的所有txt文件,显示文件名,并打包压缩成zip文件,而且还要复制该文件并进行重命名。
刚开始做一脸懵逼,经过耐心的网上查询大神资料后,终于完成了,还附加了修改文件属性,把文件改为隐藏或非隐藏状态。
附代码:
public static void setAttribute(string currPath, string fileName) //设置文件属性
{
string filePath = Path.Combine(currPath, fileName);
if (!File.Exists(filePath))
return;
if ((File.GetAttributes(filePath) & FileAttributes.Hidden) == FileAttributes.Hidden) //显示文件
{
File.SetAttributes(filePath, FileAttributes.Archive);
}
else //隐藏文件
{
File.SetAttributes(filePath, File.GetAttributes(filePath)| FileAttributes.Hidden);
}
}
public static void fileReName(string sourceFileName, string currPath,string newName) //文件重命名并复制到指定目录下
{
if (File.Exists(Path.Combine(currPath, newName)))
{
return;
}
string destinationFileName = Path.Combine(currPath, newName);
FileInfo fileInfo = new FileInfo(sourceFileName);
fileInfo.CopyTo(destinationFileName);
}
public static bool IsExistDirectory(string directoryPath) //判断是否存在文件夹
{
return Directory.Exists(directoryPath);
}
public static string[] GetFileName(string directoryPath, string searchPattern, bool isSearchChild) //获取文件名,返回字符串数组
{
if (!IsExistDirectory(directoryPath))
{
throw new FileNotFoundException();
}
try
{
return Directory.GetFiles(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
}
catch
{
return null;
}
}
public static void deleteFile(string filePath) //删除指定文件
{
if (!File.Exists(filePath))
return;
FileInfo fi = new FileInfo(filePath);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) //判断文件属性是否为只读,如果是则修改属性
fi.Attributes = FileAttributes.Normal;
File.Delete(filePath);
}
public static void creatDirectory(string path) //创建文件夹,如果不存在就创建,存在就返回
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
else
return;
}
public static void copyFile(string source,string currentPath) //把文件复制到文件夹中
{
FileInfo file = new FileInfo(source);
string destination = Path.Combine(currentPath, file.Name);
if (destination == source)
return;
if (File.Exists(destination)) //判断文件是否存在
return;
file.CopyTo(destination);
}
public static void zipFile(string strFile,string strZip)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
s.SetLevel(9); //压缩等级
zip(strFile, s, strZip);
s.Finish();
s.Close();
}
//strZip变量为压缩后的文件名,strFile为要压缩的文件名
public static void zip(string strFile, ZipOutputStream s, string strZip) //把文件夹压缩到与文件夹相同目录下
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
zip(file, s, strFile);
}
else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(strFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
s.Close();
string fileName = System.Environment.CurrentDirectory;
fileName = fileName+@"\"+ strZip;
copyFile(fileName, @"E:\"); //压缩并复制到与文件夹同路径,可以改变
}
}