/// <summary> /// 文件流压缩解压 /// </summary> public class ZipHelper { public static int BEST_COMPRESSION = 9; public static int BEST_SPEED = 1; public static int DEFAULT_COMPRESSION = -1; public static int NO_COMPRESSION = 0; #region Deflate压缩 #region Deflate压缩 /// <summary> /// Deflate方式压缩(默认压缩级别最高) /// </summary> /// <param name="stream"></param> /// <returns></returns> public static Stream Deflate(Stream stream) { return ZipHelper.Deflate(stream, ZipHelper.DEFAULT_COMPRESSION); } /// <summary> /// Deflate方式压缩 /// </summary> /// <param name="stream"></param> /// <param name="level">压缩品质级别(0~9)</param> /// <returns></returns> public static Stream Deflate(Stream stream, int level) { byte[] array = ZipHelper.StreamToBytes(stream); byte[] array2 = new byte[array.Length]; Deflater deflater = new Deflater(); deflater.SetLevel(level); deflater.SetStrategy(DeflateStrategy.Default); deflater.SetInput(array); deflater.Finish(); int num = deflater.Deflate(array2); byte[] array3 = new byte[num]; Array.Copy(array2, array3, num); return ZipHelper.BytesToStream(array3); } /// <summary> /// Deflate方式压缩 /// </summary> /// <param name="input"></param> /// <param name="level">压缩品质级别(0~9)</param> /// <returns></returns> public static byte[] Deflate(byte[] input, int level) { byte[] result; try { if (input == null && input.Length == 0) { result = new byte[0]; } else { byte[] array = new byte[input.Length]; Deflater deflater = new Deflater(); deflater.SetLevel(level); deflater.SetStrategy(DeflateStrategy.Default); deflater.SetInput(input); deflater.Finish(); int num = deflater.Deflate(array); byte[] array2 = new byte[num]; Array.Copy(array, array2, num); result = array2; } } catch (Exception innerException) { throw new Exception("压缩程序出错!", innerException); } return result; } #endregion #region Inflate解压 /// <summary> /// Inflate解压 /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] Inflate(byte[] input) { byte[] result; try { if (input == null && input.Length == 0) { result = new byte[0]; } else { Inflater inflater = new Inflater(); inflater.SetInput(input); byte[] array = new byte[1024]; using (MemoryStream memoryStream = new MemoryStream()) { for (int i = inflater.Inflate(array, 0, array.Length); i > 0; i = inflater.Inflate(array, 0, array.Length)) { memoryStream.Write(array, 0, i); } byte[] buffer = memoryStream.GetBuffer(); memoryStream.Close(); result = buffer; } } } catch (Exception innerException) { throw new Exception("解压缩程序出错!", innerException); } return result; } /// <summary> /// Inflate解压 /// </summary> /// <param name="zipStream"></param> /// <returns></returns> public static Stream Inflate(Stream zipStream) { byte[] input = ZipHelper.StreamToBytes(zipStream); byte[] bytes = ZipHelper.Inflate(input); return ZipHelper.BytesToStream(bytes); } #endregion #endregion #region GZip压缩 /// <summary> /// GZip压缩 /// </summary> /// <param name="srcStream"></param> /// <param name="output"></param> public static void GZipCompress(Stream srcStream, Stream output) { ZipHelper.GZipCompress(srcStream, 6, output); } /// <summary> /// GZip压缩 /// </summary> /// <param name="srcStream"></param> /// <param name="compressLevel">压缩品质级别(0~9)</param> /// <param name="output"></param> public static void GZipCompress(Stream srcStream, int compressLevel, Stream output) { if (compressLevel < 1 || compressLevel > 9) { throw new Exception(string.Format("您指定的压缩级别 {0} 不在有效的范围(1-9)内", compressLevel)); } srcStream.Position = 0L; GZipOutputStream gZipOutputStream = new GZipOutputStream(output); gZipOutputStream.SetLevel(compressLevel); try { int i = 4096; byte[] buffer = new byte[i]; while (i > 0) { i = srcStream.Read(buffer, 0, i); gZipOutputStream.Write(buffer, 0, i); } } catch (Exception ex) { throw new Exception("GZip压缩出错:" + ex.Message); } srcStream.Close(); gZipOutputStream.Finish(); } /// <summary> /// GZip解压 /// </summary> /// <param name="zipStream"></param> /// <param name="outputStream"></param> public static void GZipDeCompress(Stream zipStream, Stream outputStream) { GZipInputStream gZipInputStream = new GZipInputStream(zipStream); try { int i = 4096; byte[] buffer = new byte[i]; while (i > 0) { i = gZipInputStream.Read(buffer, 0, i); outputStream.Write(buffer, 0, i); } } catch (Exception ex) { throw new Exception("GZip解压缩出错:" + ex.Message); } zipStream.Close(); gZipInputStream.Close(); } #endregion #region BZip2压缩 /// <summary> /// BZip2压缩 /// </summary> /// <param name="inStream"></param> /// <param name="outStream"></param> /// <param name="blockSize"></param> public static void BZip2Compress(Stream inStream, Stream outStream, int blockSize) { BZip2.Compress(inStream, outStream, blockSize); } /// <summary> /// BZip2解压 /// </summary> /// <param name="inStream"></param> /// <param name="outStream"></param> public static void BZip2Decompress(Stream inStream, Stream outStream) { BZip2.Decompress(inStream, outStream); } #endregion private static byte[] StreamToBytes(Stream stream) { byte[] array = new byte[stream.Length]; stream.Seek(0L, SeekOrigin.Begin); stream.Read(array, 0, array.Length); stream.Close(); return array; } private static Stream BytesToStream(byte[] bytes) { return new MemoryStream(bytes); } private static void StreamToFile(Stream stream, string fileName) { byte[] array = new byte[stream.Length]; stream.Read(array, 0, array.Length); stream.Seek(0L, SeekOrigin.Begin); FileStream fileStream = new FileStream(fileName, FileMode.Create); BinaryWriter binaryWriter = new BinaryWriter(fileStream); binaryWriter.Write(array); binaryWriter.Close(); fileStream.Close(); } private static Stream FileToStream(string fileName) { FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] array = new byte[fileStream.Length]; fileStream.Read(array, 0, array.Length); fileStream.Close(); return new MemoryStream(array); } }
The above is the content of C# file stream compression and decompression. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

C# is a programming language, while .NET is a software framework. 1.C# is developed by Microsoft and is suitable for multi-platform development. 2..NET provides class libraries and runtime environments, and supports multilingual. The two work together to build modern applications.

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
