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:\"); //壓縮並複製到與文件夾同路徑,可以改變
}
}