Diễn đàn    Dotnet framework    Nén một file trong dotnet 2.0

Thành viênTrả lời
radiogaga

Ta đây thủy tinh, có tài gọi mưa hô gió ... Sơn Tinh ta đây : có tài là đốn gốc cây, đánh hốc cây ...
45  bài
4-5-2008 22:50:43
cái này thì công nhận hay thật, chia sẻ cho bà con coi
-----------------------

public void CompressFile ( string sourceFile, string destinationFile )
{
// make sure the source file is there
if ( File.Exists ( sourceFile ) == false )
throw new FileNotFoundException ( );

// Create the streams and byte arrays needed
byte[] buffer = null;
FileStream sourceStream = null;
FileStream destinationStream = null;
GZipStream compressedStream = null;

try
{
// Read the bytes from the source file into a byte array
sourceStream = new FileStream ( sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read );

// Read the source stream values into the buffer
buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read ( buffer, 0, buffer.Length );

if ( checkCounter != buffer.Length )
{
throw new ApplicationException ( );
}

// Open the FileStream to write to
destinationStream = new FileStream ( destinationFile, FileMode.OpenOrCreate, FileAccess.Write );

// Create a compression stream pointing to the destiantion stream
compressedStream = new GZipStream ( destinationStream, CompressionMode.Compress, true );

// Now write the compressed data to the destination file
compressedStream.Write ( buffer, 0, buffer.Length );
}
catch ( ApplicationException ex )
{
MessageBox.Show ( ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
// Make sure we allways close all streams
if ( sourceStream != null )
sourceStream.Close ( );

if ( compressedStream != null )
compressedStream.Close ( );

if ( destinationStream != null )
destinationStream.Close ( );
}
}
 
radiogaga

Ta đây thủy tinh, có tài gọi mưa hô gió ... Sơn Tinh ta đây : có tài là đốn gốc cây, đánh hốc cây ...
45  bài
4-5-2008 22:52:26
Đơn giản nó chỉ bao gồm:
đầu tiên tạo 1 stream byte từ file source tống nó vô buffer,
tạo stream gzip
dùng stream này tống cái buffer vào stream của thằng destination.

Chuối nhỉ ?
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM