c# - 如何使用 C# 重命名 .rar .7z、.tar、.zip 中的文件和文件夹
问题描述
我有一个压缩文件 .rar .7z、.tar 和 .zip,我想重命名使用 C# 归档的上述压缩文件中可用的物理文件名。
我已经尝试过使用sharpcompress 库,但我在.rar .7z、.tar 和.zip 文件中找不到用于重命名文件或文件夹名称的功能。
我也尝试过使用 DotNetZip 库,但它只支持。Zip 看看我使用 DotNetZip 库尝试过什么。
private static void RenameZipEntries(string file)
{
try
{
int renameCount = 0;
using (ZipFile zip2 = ZipFile.Read(file))
{
foreach (ZipEntry e in zip2.ToList())
{
if (!e.IsDirectory)
{
if (e.FileName.EndsWith(".txt"))
{
var newname = e.FileName.Split('.')[0] + "_new." + e.FileName.Split('.')[1];
e.FileName = newname;
e.Comment = "renamed";
zip2.Save();
renameCount++;
}
}
}
zip2.Comment = String.Format("This archive has been modified. {0} files have been renamed.", renameCount);
zip2.Save();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
但实际上与上面相同,我也想要 .7z、.rar 和 .tar,我尝试了很多库,但仍然没有得到任何准确的解决方案。
请帮我。
解决方案
这是一个简单的控制台应用程序,用于重命名 .zip 中的文件
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
namespace Renamer
{
class Program
{
static void Main(string[] args)
{
using var archive = new ZipArchive(File.Open(@"<Your File>.zip", FileMode.Open, FileAccess.ReadWrite), ZipArchiveMode.Update);
var entries = archive.Entries.ToArray();
//foreach (ZipArchiveEntry entry in entries)
//{
// //If ZipArchiveEntry is a directory it will have its FullName property ending with "/" (e.g. "some_dir/")
// //and its Name property will be empty string ("").
// if (!string.IsNullOrEmpty(entry.Name))
// {
// var newEntry = archive.CreateEntry($"{entry.FullName.Replace(entry.Name, $"{RandomString(10, false)}{Path.GetExtension(entry.Name)}")}");
// using (var a = entry.Open())
// using (var b = newEntry.Open())
// a.CopyTo(b);
// entry.Delete();
// }
//}
Parallel.ForEach(entries, entry =>
{
//If ZipArchiveEntry is a directory it will have its FullName property ending with "/" (e.g. "some_dir/")
//and its Name property will be empty string ("").
if (!string.IsNullOrEmpty(entry.Name))
{
ZipArchiveEntry newEntry = archive.CreateEntry($"{entry.FullName.Replace(entry.Name, $"{RandomString(10, false)}{Path.GetExtension(entry.Name)}")}");
using (var a = entry.Open())
using (var b = newEntry.Open())
a.CopyTo(b);
entry.Delete();
}
});
}
//To Generate random name for the file
public static string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
}
}
推荐阅读
- select - 样式=“宽度:100%;” 不适用于
- django - ModelForm 忽略 MultiChoice 字段的 Django 初始值
- java - StartupError > 如果 XML 文件有 ${<>} 则服务器未启动
- json - Angular 4/5 - 根据标题过滤下拉菜单
- jq - 过滤带有句点的属性名称
- angularjs - .htaccess 不重定向(版本 1.6.10)
- html - 引导程序中具有不同图像大小的图像库
- delphi - Delphi - 使用 TEdit.tagObject 指向另一个对象
- android - OpenGL ES:在顶点着色器中计算顶点位置时性能不佳
- php - phpmyadmin 无法登录 mysql 服务器