我想壓縮文件,並在C#中的目錄。我在互聯網上找到了一些解決方案,但它們非常複雜,我無法在我的項目中運行它們。任何人都可以向我建議一個清晰有效的解決方案嗎如何壓縮文件
如何壓縮文件
回答
非常感謝您提出的解決方案。我正在研究它。 – selentoptas
@aprogrammer不客氣。我很抱歉沒有發佈一些代碼,但我不知道我的確切位置,我現在沒有太多時間 – CarlosJ
使用http://dotnetzip.codeplex.com/ zip文件或目錄,也沒有內建類直接在.NET做
謝謝@Arnaud F.我看到這個,但是我的程序不識別ZipFile。我如何在我的程序中描述它? – selentoptas
有在System.IO.Packaging
一個內置類稱爲ZipPackage
:
http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage(v=vs.100).aspx
在.NET 2.0中沒有可用的用戶請求 –
您可以在System.IO.Compression
名稱空間中使用GZipStream
.NET 2.0.
public static void CompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".gz");
byte[] buffer = new byte[sourceFile.Length];
sourceFile.Read(buffer, 0, buffer.Length);
using (GZipStream output = new GZipStream(destinationFile,
CompressionMode.Compress))
{
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
destinationFile.Name, false);
output.Write(buffer, 0, buffer.Length);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
.NET 4
public static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and
// already compressed files.
if ((File.GetAttributes(fi.FullName)
& FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".gz")
{
// Create the compressed file.
using (FileStream outFile =
File.Create(fi.FullName + ".gz"))
{
using (GZipStream Compress =
new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into
// the compression stream.
inFile.CopyTo(Compress);
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}
}
感謝您的解決方案,但是我在這段代碼中有一個關於「CopyTo」的錯誤。我無法解決這個問題。 – selentoptas
@Romil - 不,命名空間在2.0中可用,我檢查過。您也可以在以下網址找到證明:http://msdn.microsoft.com/en-us/library/ms404280(v=vs.80).aspx –
@DarrenDavies,uow是正確的,它在2.0中引入。我讀錯了,我需要更多的咖啡。 –
Source code taken from MSDN that is compatible to .Net 2.0 and above
public static void CompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".gz");
byte[] buffer = new byte[sourceFile.Length];
sourceFile.Read(buffer, 0, buffer.Length);
using (GZipStream output = new GZipStream(destinationFile,
CompressionMode.Compress))
{
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
destinationFile.Name, false);
output.Write(buffer, 0, buffer.Length);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
我試圖使用這個剪切,但我有一個關於到達文件路徑的問題。我有一個關於它的錯誤?什麼是不能到達文件路徑的原因?您使用的是Windows 7 – selentoptas
。 –
是的,我使用的是Windows7。 – selentoptas
你可以使用MS-DOS命令在線程序compact.exe。 查找上的參數compact.exe在cmd並使用.NET方法的Process.Start(啓動此過程)。
我加入這個答案,因爲我發現比現有的任何答案更簡單的方法:
- 在解決方案安裝DotNetZip的DLL(最簡單的方法就是安裝the package從的NuGet)
- 添加對DLL的引用。
- 通過添加導入名稱空間:使用Ionic.Zip;
- 郵編文件
代碼:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("C:\test\test.txt");
zip.AddFile("C:\test\test2.txt");
zip.Save("C:\output.zip");
}
如果你不想在zip文件反映了原來的文件夾結構,然後再看看的替換值AddFile()和AddFolder()等
使用DotNetZip http://dotnetzip.codeplex.com/,還有對zip文件類的AddDirectory()方法,你想要做什麼:
using (var zip = new Ionic.Zip.ZipFile())
{
zip.AddDirectory("DirectoryOnDisk", "rootInZipFile");
zip.Save("MyFile.zip");
}
Bonne繼續...
只需使用以下代碼來壓縮文件。
public void Compressfile()
{
string fileName = "Text.txt";
string sourcePath = @"C:\SMSDBBACKUP";
DirectoryInfo di = new DirectoryInfo(sourcePath);
foreach (FileInfo fi in di.GetFiles())
{
//for specific file
if (fi.ToString() == fileName)
{
Compress(fi);
}
}
}
public static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and
// already compressed files.
if ((File.GetAttributes(fi.FullName)
& FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".gz")
{
// Create the compressed file.
using (FileStream outFile =
File.Create(fi.FullName + ".gz"))
{
using (GZipStream Compress =
new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into
// the compression stream.
inFile.CopyTo(Compress);
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}
}
}
- 1. 如何壓縮文件時不壓縮
- 2. 如何將未壓縮文件夾視爲壓縮文件?
- 3. 如何直接將csv文件壓縮成zip壓縮文件?
- 4. 如何壓縮XML文件?
- 5. 如何壓縮Excel文件?
- 6. 如何壓縮文件
- 7. 如何解壓/解壓縮* .z文件
- 8. 如何解壓/解壓縮文件
- 9. NSIS:如何解壓7zip壓縮文件
- 10. 如何只壓縮文件夾內的文件而不壓縮子文件夾?
- 11. 壓縮文件
- 12. 如何壓縮文本文件
- 13. 如何壓縮文本文件
- 14. 如何(高度)壓縮文件並解壓縮iOS
- 15. node.js如何流化22GB gzip壓縮文件解壓縮
- 16. 如何查找離子壓縮文件的未壓縮大小
- 17. 如何在內存中解壓縮GZip壓縮文件?
- 18. 如何啓用HTTP壓縮來壓縮JSON文件
- 19. 如何解壓縮已壓縮兩次的文件?
- 20. 如何生成文件,然後用Heroku壓縮/壓縮?
- 21. 如何使用AngularJs解壓縮壓縮文件?
- 22. 如何使用C#解壓縮壓縮文件?
- 23. 如何使用命令行解壓縮xip壓縮文件?
- 24. 如何使用Zend壓縮過濾器壓縮多個文件?
- 25. 如何解壓縮使用fusecompress/lzo壓縮的文件?
- 26. 如何使用rubyzip解壓縮壓縮文件夾
- 27. 如何在asp.net框架4.0中壓縮/解壓縮文件夾
- 28. 如何將「tail -f」壓縮成壓縮(gziped)文件?
- 29. 壓縮文件未解壓
- 30. Node.js - 壓縮/解壓縮文件夾
爲什麼不能在你的項目中運行它們? – BugFinder
是指在.NET 4.0 http://msdn.microsoft.com/en-us/library/ms404280.aspx –
可能重複[如何將目錄壓縮成zip文件編程(HTTP://計算器。com/questions/2498572 /如何壓縮目錄到一個zip文件編程) – ken2k