当前位置:网站首页>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();
}
}
边栏推荐
- #981 Time Based Key-Value Store
- Shell language
- Cookies and sessions
- GNS installation and configuration
- SQL调优指南笔记17:Importing and Exporting Optimizer Statistics
- Can tonghuashun open an account? Is it safe to open an account in tonghuashun
- The Post-00 financial woman with a monthly salary of 2W conquered the boss with this set of report template
- Smart management of green agriculture: a visual platform for agricultural product scheduling
- Ubuntu16.04 completely delete MySQL database
- A high-value MySQL management tool
猜你喜欢

Deep Hough voting for 3D object detection in point clouds

Mxnet record IO details
![[target detection] |dive detector into box for object detection new training method based on fcos](/img/ac/c54c2733dceffea086b772f35f128a.png)
[target detection] |dive detector into box for object detection new training method based on fcos

Composer version degradation

torch. Finfo function

JUC并发工具包使用指南

【目标检测】|Dive Deeper Into Box for Object Detection 基于FCOS新训练方法

结构体知识点all in

Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"

Leetcode: 210. Programme II
随机推荐
torch. nn. Linear() function
#113 Path Sum II
Test basis: unit test
ICML2022 | GALAXY:极化图主动学习
Kdd2022 | graphmae: self supervised mask map self encoder
Cookies and sessions
Research Report on hydraulic solenoid valve industry - market status analysis and development prospect forecast
Access control system based on RFID
lintcode:127 · 拓扑排序
Vs2017 environmental issues
Is it safe to open an account in flush? How to open an account online to buy stocks
#113 Path Sum II
ORM implements the mapping relationship between classes and tables, class attributes and fields
杨辉三角代码实现
Libmysqlclient A static library
Digital intelligence data depth | Bi goes down the altar? It's not that the market has declined, it's that the story has changed
Introduction to the characteristics of balancer decentralized exchange market capitalization robot
JUC并发工具包使用指南
Ubuntu 16.04 installing mysql5.6
Research Report on market supply and demand and strategy of China's hydraulic hammer industry