java gzipoutputstream压缩比例大概是多少
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java gzipoutputstream压缩比例大概是多少相关的知识,希望对你有一定的参考价值。
参考技术A It is my decision,It is my life.JAVA对字符串的压缩与解压缩
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtils
/**
* 使用gzip进行压缩
*/
public static String gzip(String primStr)
if (primStr == null || primStr.length() == 0)
return primStr;
本回答被提问者采纳
java程序压缩和解压zip文件
需要了解的知识汇总:
DeflaterOutputStream :压缩类的基类。
ZipOutputStream :DeflaterOutputStream的一个子类,把数据压缩成Zip文件格式
GZIPOutputStream :DeflaterOutputStream的一个子类,把数据压缩成GZip文件格式
InflaterInputStream :解压缩类的基类
ZipInputStream :InflaterInputStream的一个子类,能解压缩Zip格式的数据
GZIPInputStream :InflaterInputStream的一个子类,能解压缩Zip格式的数据
ZipEntry 类:表示 ZIP 文件条目
ZipFile 类:此类用于从 ZIP 文件读取条目
以zipentry来命名zip内文件列表。
以FileInputStream来读取文件。
以zipoutputstream输出流进行压缩文件写入。
逻辑简单好懂,附件带有实例,解压即可运行。
百度网盘实例链接:https://pan.baidu.com/s/1VSs-UqtU_3Lld2H7-1dMMg
提取码:mcb3
package com.crscd.server.web.cbtc2.filezip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class FileZip {
/*
-
author:zhao_anan 20190424 zip compress program
/
/public void zip(ZipOutputStream out, File f, String base, boolean first)
throws Exception {
if (first) {
if (f.isDirectory()) {
out.putNextEntry(new ZipEntry("/"));
base = base + f.getName();
first = false;
} else
base = f.getName();
}
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName(), first);
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}*//*
-
unzip
/
/public void unZipFileByOpache(ZipFile zipFile, String unZipRoot)
throws Exception, IOException {
Enumeration<?> e = zipFile.entries();
ZipEntry zipEntry;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
InputStream fis = zipFile.getInputStream(zipEntry);
if (zipEntry.isDirectory()) {
} else {
File file = new File(unZipRoot + File.separator- zipEntry.getName());
System.out.println(unZipRoot + File.separator - zipEntry.getName());
File parentFile = file.getParentFile();
parentFile.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b, 0, b.length)) != -1) {
fos.write(b, 0, len);
}
fos.close();
fis.close();
}
}
}
public void ZipFile(String zipFileName, String inputFileName)
throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
File inputFile = new File(inputFileName);
zip(out, inputFile, "", true);
System.out.println("zip done");
out.close();
}public void unZipFile(String unZipFileName, String unZipPath)
throws Exception {
//ZipFile zipFile = new ZipFile(unZipFileName);
//unZipFileByOpache(zipFile, unZipPath);
System.out.println("unZip Ok");
}public static void main(String[] args) throws Exception {
FileZip fileZip = new FileZip();
// ZipFile("d:/testfile.zip","d:/TTPlayer");
fileZip.unZipFile("d:/20120206_SLIS_0002_DY2.zip", "d:/");
}*//**
- zipEntry.getName());
- unzip file 20190424
- @auther: zhao_anan
- @param unzipRoot
-
*/
public void unZipFile(String zipFilename, String unzipRoot)
throws Exception {
BufferedOutputStream bos = null;
FileInputStream fis = new FileInputStream(zipFilename);
BufferedInputStream bis = new BufferedInputStream(fis);ZipInputStream zis = new ZipInputStream(bis); ZipEntry entry = null; int count = 0; byte buf[] = new byte[1024]; while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory()) { } else { File file = new File(unzipRoot + File.separator + entry.getName()); File parentFile = file.getParentFile(); parentFile.mkdirs(); bos = new BufferedOutputStream(new FileOutputStream(file)); while ((count = zis.read(buf)) != -1) { bos.write(buf, 0, count); } bos.flush(); bos.close(); } } zis.close();
}
/** - zip
- @param zipFilename
- @param unzipRoot
-
*/
private void zip(ZipOutputStream out, File f, String base, boolean first)
throws Exception {
if (first) {
if (f.isDirectory()) {
out.putNextEntry(new ZipEntry("/"));
base = base + f.getName();
first = false;
} else
base = f.getName();
}
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName(), first);
}
} else {
out.putNextEntry(new ZipEntry(base));
// FileInputStream in = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(f), "ISO8859_1"));
int b;
System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}public void zipFile(String zipFileName, String inputFileName)
throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
File inputFile = new File(inputFileName);
zip(out, inputFile, "", true);
System.out.println("zip done");
out.close();
}/**
- zip
- @param zipFilename
- @param unzipRoot
-
*/
private void zipList(ZipOutputStream out, String[] fileList, String base)
throws Exception {
base = base + "\";
for (int i = 0; i < fileList.length; i++) {
//zip(out, , base + fileList[i], first);
out.putNextEntry(new ZipEntry(fileList[i]));
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(base + fileList[i]), "ISO8859_1"));
int b;
System.out.println(base + fileList[i]);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}public void zipListFile(String zipFileName, String inputListFiles, String baseurl)
throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
String[] FileList = inputListFiles.split(",");
zipList(out, FileList, baseurl);
System.out.println("zip done");
out.close();
}public static void main(String[] args) {
//压缩测试 FileZip tFileZip = new FileZip(); String tinputFileName = "E:\\temp"; String baseurl = "E:\\temp"; String tzipFileName = "E:\\temp.zip"; String inputListFiles = "application-dev.yml,auto_arrange.yml"; try { //tFileZip.zipFile(tzipFileName, tinputFileName); tFileZip.zipListFile(tzipFileName, inputListFiles, baseurl); //tFileZip.unZipFile(tzipFileName, baseurl + "unzip"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
}
以上是关于java gzipoutputstream压缩比例大概是多少的主要内容,如果未能解决你的问题,请参考以下文章