C#에서 파일과 폴더를 빠르게 압축하거나 압축 해제하는 좋은 방법을 아는 사람이 있나요? 대용량 파일을 처리해야 할 수도 있습니다.
Gzip 과 Deflate 알고리즘을 시스템드리오콤프레시온 레임워크 이름공간이 .Net 2.0 '은'. 다음은 2 바이트 스트림 압축 및 압축 파일 시스템에서 객체에는 메서드입니다 액세스할 수 있습니다. 다음 방법 '에서' 데포타슬림 조지프 스트림 서브시구티 수 있습니다 '' 을 사용하는 데 알고리즘입니다. 이 문제는 여전히 서로 다른 파일 압축 알고리즘을 어쨌든요 취급료 찻입
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();
}
저는 항상 SharpZip 라이브러리를 사용했습니다.
Tom이 지적한 대로 SharpZip과 같은 타사 라이브러리를 사용할 수 있습니다.
타사 라이브러리를 사용하지 않는 또 다른 방법은 Windows 셸 API를 사용하는 것입니다. C# 프로젝트에서 Microsoft Shell 컨트롤 및 자동화 COM 라이브러리에 대한 참조를 설정해야 합니다. 제럴드 깁슨의 예는 다음과 같습니다:
위에 언급된 바와 같이 이 매우 쉽게 수행할 수 있는 자바, C # 에서 라이브러리보다는 제바스티레비치프 꽂으십시오 도달할 수 있습니다. 대한 참조에는 지켜보리니:
제바스티레비치프 javadoc < br>; 샘플 코드
내가 할 수 있는 zip 의 깊은 (recursive) 이 사용되는 귈이예요 전 폴더 구조를 사용해 본 적이 없다, 하지만 난 don& # 39, 압축을 푼 것 같아요. 그래서 난 과민반을을 I& 경우 # 39, m 에 의해 그 코드를 당기십시오 로그아웃되며 편집하십시오 여기 있다.
이 방법을 사용하면 zip 파일을 만들 수 있습니다.
public async Task<string> 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;
}
그리고 당신이 zip 파일 압축을 풀 수 있는 이 방법: 이 방법을 작동합니까 zip 파일 경로, 1 - br /> <.
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();
});
}
이 방법은 zip 파일을 작동합니까 스트리밍합니다 2 -
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));
}