NET, code is the same; the only thing that needs to change is the class name. Compression that resides on assembly System. This namespace contains only three types, the two algorithms implementation classes DeflateStream and GZipStream -both inherit directly from System. Stream class-, and an enumeration CompressionMode that defines the operation compression or decompression. The code for compression is very simple:. Do not forget to add two using statements to namespaces System. IO and System. Create, FileAccess.
Write data, 0, data. Length ; alg. Code explanation:. If you are going to compress a file then you must specify the CompressionMode. Compress option, and also you must specify a Stream that will the data be written to.
After creating the class you can compress the data by using its Write method. If you intended to use the Deflate algorithm, just change the class name to DeflateStream. The code that decompresses a compressed file is very similar:. Open, FileAccess.
Read buffer, 0, buffer. Length ; originalFile. Improve this answer. Mauro Braggio Mauro Braggio 3 3 silver badges 7 7 bronze badges. A similar case: osdir. This is not true in general. You can always extract raster images from the file, reduce their resolution, and put them back on the file.
You can also change the compression type for lossless-compressed images and use a lossy algorithm instead. Ajinkya VahidN VahidN Morcilla de Arroz Morcilla de Arroz 1, 22 22 silver badges 27 27 bronze badges.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.
Besides algorithms classes, you may use System. BinaryWriter and System. BinaryReader to get full control of how the file contents will be.
Posted in File System Tagged:. This article, along with any associated source code and files, is licensed under The Common Public License Version 1. Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages. Technical Blog. View Original. Tagged as C. Stats Programmatically Compress and Decompress Files.
Mohammad Elsheimy Rate me:. Please Sign up or sign in to vote. Learn how to compress and decompress files programmatically using C. NET Framework supports two types of compression algorithms: Deflate This is a very simple algorithm, used for simple compression and decompression operations.
Don't use this algorithm except if you intend to use compressed data only in your application because this algorithm is not compatible with any compression tool. This algorithm includes a cyclic redundancy check value for detecting data corruption. This algorithm also can be extended to use other algorithms internally other than Deflate. Compressing a File The code for compression is very simple: Code snippets in this lesson rely on the fact that you added two using Imports in VB statements, System.
Copy Code. Mohammad Elsheimy.
0コメント