【发布时间】:2014-03-12 19:53:13
【问题描述】:
我在一个目录中有很多同名的文件。我想压缩(zip 等)MyDirectory 中最后修改的文件。你能帮帮我吗?
我可以压缩一个文件,但不能压缩最后修改的文件:
private void button1_Click(object sender, EventArgs e)
{
FileStream sourceFile = File.OpenRead(@"C:\MyDirectory");
FileStream destFile = File.Create(@"C:\MyDirectory.zip");
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
try
{
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
}
finally
{
compStream.Dispose();
}
MessageBox.Show("file is compressed successfully");
}
【问题讨论】:
-
如果你有 .NET 4.5,你可以使用
System.IO.Compression.ZipFile类。 -
您运行的是什么操作系统?最后我检查了 Windows 在给定目录中所需的唯一文件名。注意:
new FileInfo("foo.txt").LastWriteTime会告诉你最后修改时间。 -
如果您使用的是 4.5,有一个不错的库可以用于压缩部分:System.IO.Compression
-
.NET 4.5 我有。我可以用上面的代码压缩。但我不知道获取最新修改的文件并压缩它。MyDirectory 是一个示例名称。它的真实名称是 luigiFiles。我认为这并不重要
-
我很惊讶没有人问你如何在同一个目录中有多个同名文件?
标签: c# .net compression last-modified