문제

정기적으로 콘텐츠를 다운로드하고 추출하고 저장해야 합니다. http://data.dot.state.mn.us/dds/det_sample.xml.gz 디스크에.C#으로 gzip 파일을 다운로드해 본 경험이 있는 사람이 있나요?

도움이 되었습니까?

해결책

압축하려면:

using (FileStream fStream = new FileStream(@"C:\test.docx.gzip", 
FileMode.Create, FileAccess.Write)) {
    using (GZipStream zipStream = new GZipStream(fStream, 
    CompressionMode.Compress)) {
        byte[] inputfile = File.ReadAllBytes(@"c:\test.docx");
        zipStream.Write(inputfile, 0, inputfile.Length);
    }
}

압축을 풀려면:

using (FileStream fInStream = new FileStream(@"c:\test.docx.gz", 
FileMode.Open, FileAccess.Read)) {
    using (GZipStream zipStream = new GZipStream(fInStream, CompressionMode.Decompress)) {   
        using (FileStream fOutStream = new FileStream(@"c:\test1.docx", 
        FileMode.Create, FileAccess.Write)) {
            byte[] tempBytes = new byte[4096];
            int i;
            while ((i = zipStream.Read(tempBytes, 0, tempBytes.Length)) != 0) {
                fOutStream.Write(tempBytes, 0, i);
            }
        }
    }
}

C# 및 내장 GZipStream 클래스를 사용하여 gzip 파일의 압축을 푸는 방법을 보여 주는 작년에 작성한 게시물에서 가져왔습니다.http://blogs.msdn.com/miah/archive/2007/09/05/zipping-files.aspx

다운로드는 표준을 사용할 수 있습니다 웹요청 또는 웹클라이언트 .NET의 클래스.

다른 팁

System.Net에서 WebClient를 사용하여 다음을 다운로드할 수 있습니다.

WebClient Client = new WebClient ();
Client.DownloadFile("http://data.dot.state.mn.us/dds/det_sample.xml.gz", " C:\mygzipfile.gz");

그런 다음 사용 #ziplib 추출하다

편집하다:또는 GZipStream...그거 잊어버렸어

시도해 보세요 SharpZipLib, gzip/zip을 사용하여 파일을 압축 및 압축 해제하기 위한 C# 기반 라이브러리입니다.

샘플 사용법은 여기에서 확인할 수 있습니다. 블로그 게시물:

using ICSharpCode.SharpZipLib.Zip;

FastZip fz = new FastZip();       
fz.ExtractZip(zipFile, targetDirectory,"");

그냥 사용 HttpWeb요청 System.Net 네임스페이스의 클래스를 사용하여 파일을 요청하고 다운로드합니다.그런 다음 사용 GZipStream System.IO.Compression 네임스페이스의 클래스를 사용하여 지정한 위치에 콘텐츠를 추출합니다.그들은 예를 제공합니다.

그만큼 GZipStream 수업은 당신이 원하는 것일 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top