当前位置:网站首页>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();
}
}
边栏推荐
- Risk control modeling X: Discussion on problems existing in traditional modeling methods and Exploration on improvement methods
- Market trend report, technical innovation and market forecast of hydraulic chain hoist in China
- ATOI super resolution
- Image processing 12- image linear blending
- Limit of advanced mathematics
- Jdbctemplate inserts and returns the primary key
- leetcode:210. Schedule II
- To delete a character from a string
- KDD2022 | GraphMAE:自监督掩码图自编码器
- Recursively call knowledge points - including example solving binary search, frog jumping steps, reverse order output, factorial, Fibonacci, Hanoi tower.
猜你喜欢

leetcode:210. 課程錶 II

Distributed cloud service developer'allegro Xile technology 'received an angel round financing of US $3million

SQL调优指南笔记16:Managing Historical Optimizer Statistics

图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?

Deep Hough voting for 3D object detection in point clouds

Graphics2D类基本使用

KDD2022 | GraphMAE:自监督掩码图自编码器

利用ADG Standby克隆PDB

Shell script Basics

一款高颜值的MySQL管理工具
随机推荐
A blog written clearly by vit
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
Redis cluster mget optimization
Pytorch how to set random number seed to make the result repeatable
DRF receives nested data and creates objects. Solution: DRF not NULL constraint failed
Module 8: Design message queue MySQL table for storing message data
Gather function in pytorch_
ATOI super resolution
String Basics
The year of the outbreak of financial innovation! All dtinsight products of kangaroo cloud data stack have passed the special test of Xinchuang of ICT Institute
复杂系统如何检测异常?北卡UNCC等最新《复杂分布式系统中基于图的深度学习异常检测方法综述》,阐述最新图异常检测技术进展
Test basis: unit test
Experiment 7-2-6 print Yanghui triangle (20 points)
NIO使用指南
图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?
Kdd2022 | graphmae: self supervised mask map self encoder
atoi超强解析
Distributed cloud service developer'allegro Xile technology 'received an angel round financing of US $3million
Can tonghuashun open an account? Is it safe to open an account in tonghuashun
Libmysqlclient A static library