java用Inflater解压zip的代码,C#如何实现
时间: 2024-09-11 08:13:27 浏览: 83
在Java中,可以使用`java.util.zip`包下的`InflaterInputStream`类配合`ZipInputStream`来解压缩`.zip`文件。下面是一个简单的示例:
```java
import java.io.*;
import java.util.zip.*;
public class ZipDecompression {
public static void main(String[] args) {
try {
File zipFile = new File("path_to_your_zip_file.zip");
FileInputStream fileIn = new FileInputStream(zipFile);
// 创建InflaterInputStream并传入原输入流
InflaterInputStream inflaterIn = new InflaterInputStream(fileIn);
// 创建ZipInputStream处理解压内容
ZipInputStream zin = new ZipInputStream(inflaterIn);
// 读取压缩文件中的条目
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
String filePath = entry.getName();
// 打开对应的输出流
File outputFile = new File(zipFile.getParent(), filePath);
FileOutputStream out = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = zin.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
// 关闭流
zin.closeEntry();
out.close();
}
// 最后关闭所有资源
zin.close();
inflaterIn.close();
fileIn.close();
System.out.println("Decompression completed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在C#中,你可以使用.NET Framework提供的`System.IO.Compression.ZipFile`类来解压缩`.zip`文件,示例如下:
```csharp
using System.IO;
using System.IO.Compression;
class Program
{
static void Main(string[] args)
{
string zipFilePath = "path_to_your_zip_file.zip";
string extractTo = "extract_folder_path";
using (var archive = ZipFile.OpenRead(zipFilePath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string entryPath = Path.Combine(extractTo, entry.FullName);
if (!entry.Path.EndsWith("/"))
{
Directory.CreateDirectory(entryPath);
}
else
{
File.Create(entryPath).Close(); // 创建目录
}
}
}
Console.WriteLine("Decompression completed.");
}
}
```
阅读全文