当前位置:网站首页>Gzip compression decompression
Gzip compression decompression
2022-06-12 21:33:00 【Besieged city_ city with high walls】
package com.raise.raisestudy.zip;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class GzipUtils {
/** * Compress a file or directory <br/> * Compress the file or directory into zip Format of compressed files * * @param compressedFile Compressed file * @param preCompressFile The path to a file or directory that needs to be compressed * @return true | false */
public static boolean compress(String compressedFile, String preCompressFile)
{
boolean isSucc = true;
File inputFile = new File(preCompressFile);
ZipOutputStream out;
try
{
out = new ZipOutputStream(new FileOutputStream(compressedFile));
compress(out, inputFile, inputFile.getName());
out.close();
}
catch (Exception e)
{
isSucc = false;
}
return isSucc;
}
/** * Recursively compress all files in the folder * * @param out Output stream * @param file Files that need to be compressed * @param base The parent directory of the compressed file * @throws IOException */
private static void compress(ZipOutputStream out, File file, String base)
throws IOException
{
if (file.isDirectory())
{
File[] fs = file.listFiles();
base += "/";
out.putNextEntry(new ZipEntry(base)); // Generate the corresponding directory
for (int i = 0; i < fs.length; i++)
{
// Recursively traverse all file objects in this directory , Compress one by one
compress(out, fs[i], base + fs[i].getName());
}
}
else
{
out.putNextEntry(new ZipEntry(base));
InputStream is = new FileInputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1)
{
out.write(buf, 0, len);
}
is.close();
}
}
/** * Unzip the file <br/> * decompression zip File format * @param zipFile Files that need to be decompressed * @param desPath Directory saved after decompression */
public static void decompress(String zipFile, String desPath) throws IOException
{
// Create output stream , Used to write the file stream read from the compressed file to the disk
OutputStream out = null;
// Create an input stream , Used to read files from compressed files
ZipInputStream is;
File dir = new File(desPath);
dir.mkdirs();
is = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry entry = null;
while ((entry = is.getNextEntry()) != null)
{
File f = new File(dir, entry.getName());
if (entry.isDirectory())
{
f.mkdir();
}
else
{
// Create a new file according to the file name read out in the compressed file
out = new FileOutputStream(f);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1)
{
out.write(buf, 0, len);
}
out.close();
}
}
is.close();
}
}
边栏推荐
- Ubuntu 16.04 installing mysql5.6
- SQL调优指南笔记18:Analyzing Statistics Using Optimizer Statistics Advisor
- Graphics2d class basic use
- zgc的垃圾收集的主要階段
- #886 Possible Bipartition
- 递归调用知识点-包含例题求解二分查找、青蛙跳台阶、逆序输出、阶乘、斐波那契、汉诺塔。
- Vs2017 environmental issues
- 服务没有报告任何错误mysql
- 阅读笔记 Deep Hough Voting for 3D Object Detection in Point Clouds
- Select sort
猜你喜欢

Digital intelligence data depth | Bi goes down the altar? It's not that the market has declined, it's that the story has changed
![Fill in the checklist & lt; int&gt; Have default values? [repeat] - fill list & lt; int&gt; with default values? [duplicate]](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
Fill in the checklist & lt; int&gt; Have default values? [repeat] - fill list & lt; int&gt; with default values? [duplicate]

Shell language

Composer version degradation

My way of programming

SQL调优指南笔记18:Analyzing Statistics Using Optimizer Statistics Advisor

Kdd2022 | graphmae: self supervised mask map self encoder

SQL调优指南笔记14:Managing Extended Statistics

Experiment 7-2-6 print Yanghui triangle (20 points)

Lombok package is successfully installed, but the runtime prompts that get, set method and constructor solution cannot be found
随机推荐
Two sentences to clarify JS throttling and anti shake
Access control system based on RFID
Module 8: Design message queue MySQL table for storing message data
同花顺能开户吗,在APP上可以直接开通券商安全吗
gzip压缩解压缩
有向图深拷贝
Graphics2D类基本使用
A blog written clearly by vit
selenium操作元素遇到的异常
测试基础之:单元测试
如何自己动手写一个vscode插件,实现插件自由!
求解一维数组前缀和
DRF receives nested data and creates objects. Solution: DRF not NULL constraint failed
Can flush open an account? Can you directly open the security of securities companies on the app
USB机械键盘改蓝牙键盘
二分查找
JdbcTemplate插入并返回主键
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
jsonUtils
GPU giant NVIDIA suffered a "devastating" network attack, and the number one malware shut down its botnet infrastructure | global network security hotspot on February 28