Stie cineva despre o modalitate buna de a comprima sau decomprima fisierele si folderele din C # repede? Este posibil să fie necesară manipularea unor fișiere mari.
As of .Net 1.1 the only available method is reaching into the java libraries.
Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C#
Not sure if this has changed in recent versions.
Întotdeauna am folosit Biblioteca SharpZip.
Puteți folosi o bibliotecă terță parte, cum ar fi SharpZip așa cum a subliniat Tom.
Un alt mod (fără a merge 3rd party) este utilizarea API-ului Windows Shell. Va trebui să setați o referință la biblioteca COM Shell Controls și Automation COM în proiectul dvs. C #. Gerald Gibson are un exemplu la:
http://geraldgibson.net/dnn/Home/CZipFileCompression/tabid/148/Default.aspx
Acest lucru este foarte ușor de făcut în java, și așa cum sa menționat mai sus, puteți ajunge în bibliotecile java.util.zip din C #. Pentru referințe, consultați:
java.util.zip javadocs
sample code
Am folosit acest lucru cu ceva timp în urmă pentru a face un zip profund (recursiv) al unei structuri de directoare, dar nu cred că am folosit vreodată unzipping-ul. Dacă sunt atât de motivați, pot să scot codul și să îl editez mai târziu.
Nomenclatorul de spațiu .Net 2.0 System.IO.Compression
acceptă algoritmi GZip și Deflate. Iată două metode care comprimă și decomprimă un flux de octeți pe care îl puteți obține de la obiectul fișierului. Puteți substitui GZipStream
pentru DefaultStream
în metodele de mai jos pentru a utiliza respectivul algoritm. Acest lucru lasă totuși problema manipulării fișierelor comprimate cu algoritmi diferiți.
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
GZipStream is a really good utility to use.
puteți crea fișier zip cu această metodă:
public async Task CreateZipFile(string sourceDirectoryPath, string name)
{
var path = HostingEnvironment.MapPath(TempPath) + name;
await Task.Run(() =>
{
if (File.Exists(path)) File.Delete(path);
ZipFile.CreateFromDirectory(sourceDirectoryPath, path);
});
return path;
}
and then you can unzip zip file with this methods:
1- this method work with zip file path
public async Task ExtractZipFile(string filePath, string destinationDirectoryName)
{
await Task.Run(() =>
{
var archive = ZipFile.Open(filePath, ZipArchiveMode.Read);
foreach (var entry in archive.Entries)
{
entry.ExtractToFile(Path.Combine(destinationDirectoryName, entry.FullName), true);
}
archive.Dispose();
});
}
2 - această metodă funcționează cu flux de fișiere zip
public async Task ExtractZipFile(Stream zipFile, string destinationDirectoryName)
{
string filePath = HostingEnvironment.MapPath(TempPath) + Utility.GetRandomNumber(1, int.MaxValue);
using (FileStream output = new FileStream(filePath, FileMode.Create))
{
await zipFile.CopyToAsync(output);
}
await Task.Run(() => ZipFile.ExtractToDirectory(filePath, destinationDirectoryName));
await Task.Run(() => File.Delete(filePath));
}