使用压缩流需要注意的是
压缩输入流的available方法返回的永远是1或者0
-
import java.util.zip.*;
-
import java.io.*;
-
-
public class ZipUtil {
-
public static void main(String[] args) throws IOException {
-
unzip("/home/lihuilin/桌面/src.zip", "/home/lihuilin/桌面/tar/");
-
zip("/home/lihuilin/桌面/tar/", "/home/lihuilin/桌面/target.zip");
-
}
-
-
public static void unzip(String source, String target) throws IOException {
-
File t = new File(target);
-
if (!t.exists()) {
-
t.mkdirs();
-
}
-
ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(source)));
-
String filepath = target;
-
ZipEntry entry = null;
-
int b;
-
while ((entry = in.getNextEntry()) != null) {
-
if (entry.isDirectory()) {
-
new File(filepath + entry.getName()).mkdirs();
-
} else {
-
System.out.println(filepath + entry.getName());
-
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filepath + entry.getName()));
-
while ((b = in.read()) != -1) {
-
out.write(b);
-
}
-
in.closeEntry();
-
out.flush();
-
out.close();
-
}
-
}
-
in.close();
-
-
}
-
-
public static void zip(String source, String target) throws IOException {
-
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
-
for (File file : new File(source).listFiles()) {
-
zip(file, out, source);
-
}
-
out.close();
-
-
}
-
-
private static void zip(File file, ZipOutputStream out, String filepath) throws IOException {
-
-
if (file.isDirectory()) {
-
for (File f : file.listFiles()) {
-
zip(f, out, filepath);
-
}
-
} else {
-
ZipEntry entry = new ZipEntry(file.getPath().replaceAll(filepath, ""));
-
out.putNextEntry(entry);
-
-
FileInputStream fis = new FileInputStream(file.toString());
-
int i = 0;
-
while ((i = fis.read()) != -1) {
-
out.write(i);
-
}
-
fis.close();
-
out.flush();
-
out.closeEntry();
-
}
-
-
}
-
-
}
使用GZIP压缩
-
import java.io.BufferedInputStream;
-
import java.io.BufferedOutputStream;
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.util.zip.GZIPInputStream;
-
import java.util.zip.GZIPOutputStream;
-
-
public class GZIPUtil {
-
public static void main(String[] args) throws IOException {
-
compress("C:\\Users\\new\\Desktop\\源文件.txt", "C:\\Users\\new\\Desktop\\目標文件.txt");
-
decompress("C:\\Users\\new\\Desktop\\目標文件.txt", "C:\\Users\\new\\Desktop\\解压文件.txt");
-
}
-
-
private static void decompress(String source, String target) throws IOException {
-
GZIPInputStream in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(source)));
-
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target));
-
int b = 0;
-
while ((b = in.read()) != -1) {
-
out.write(b);
-
}
-
out.flush();
-
out.close();
-
in.close();
-
}
-
-
private static void compress(String source, String target) throws IOException {
-
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
-
GZIPOutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(target)));
-
byte[] data = new byte[bis.available()];
-
bis.read(data);
-
out.write(data);
-
out.flush();
-
out.close();
-
bis.close();
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1158736/
,如需转载,请注明出处,否则将追究法律责任。