当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

Structure knowledge points all in
![[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

lintcode:127 · 拓扑排序

Smart management of green agriculture: a visual platform for agricultural product scheduling

What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones

Vs2017 environmental issues

结构体知识点all in

CVPR 2022 | 应对噪声标签,西安大略大学、字节跳动等提出对比正则化方法

#141 Linked List Cycle

一款高颜值的MySQL管理工具
随机推荐
Icml2022 | Galaxy: apprentissage actif des cartes de polarisation
Pointer and array & pointer and const & struct and Const
Limit of advanced mathematics
leetcode:210. 課程錶 II
Vs2017 environmental issues
selenium操作元素遇到的异常
SQL调优指南笔记18:Analyzing Statistics Using Optimizer Statistics Advisor
zgc 并发标识和并发转移阶段的多视图地址映射
China hydraulic cylinder linear position sensor market trend report, technical dynamic innovation and market forecast
递归调用知识点-包含例题求解二分查找、青蛙跳台阶、逆序输出、阶乘、斐波那契、汉诺塔。
Deep Hough voting for 3D object detection in point clouds
A high-value MySQL management tool
CVPR 2022 | 应对噪声标签,西安大略大学、字节跳动等提出对比正则化方法
Common error in script execution: build sh: caller: not found
#886 Possible Bipartition
SQL调优指南笔记16:Managing Historical Optimizer Statistics
Smart management of green agriculture: a visual platform for agricultural product scheduling
Lake shore PT-100 platinum resistance temperature sensor
实现从字符串中删除某个字符操作
Bubble sort