Java使用Zip壓縮
使用壓縮流需要注意的是
壓縮輸入流的available方法返回的永遠是1或者0
使用GZIP壓縮
壓縮輸入流的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部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1158736/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- zip壓縮和解壓縮
- 利用Java實現zip壓縮/解壓縮 (轉)Java
- JAVA基礎:利用Java實現zip壓縮解壓縮(轉)Java
- 使用org.apache.tools.zip實現zip壓縮和解壓Apache
- java 把檔案壓縮成 zipJava
- java 生成 zip格式 壓縮檔案Java
- zip解壓縮
- 簡單的zip壓縮和解壓縮
- zip壓縮檔案處理方案(Zip4j壓縮和解壓)
- 壓縮解壓命令(tar, zip)
- 使用java API進行zip遞迴壓縮資料夾以及解壓JavaAPI遞迴
- java實現zip壓縮檔案/資料夾Java
- 使用jar與zip壓縮解壓檔案的區別JAR
- Java實現檔案壓縮與解壓[zip格式,gzip格式]Java
- node ~ zip壓縮 && 檔案加密加密
- Ashampoo ZIP Pro 4,解壓縮
- ZipArchive解壓縮zip檔案Hive
- CentOS中zip壓縮和unzip解壓縮命令詳解CentOS
- Linux下zip壓縮和unzip解壓縮命令全解Linux
- linux命令系列-zip(壓縮打包)Linux
- java解壓rar,解壓zipJava
- Centos7中使用7zip壓縮工具CentOS
- JAVA壓縮和解壓縮Java
- CentOS7中zip壓縮和unzip解壓縮命令詳解CentOS
- Linux科研武器庫 - 檔案壓縮與解壓縮 - zip / unzipLinux
- Windows的壓縮資料夾(zip/cab)Windows
- Android總結之Gzip/Zip壓縮Android
- Android Java壓縮Zlib,Gzip,Zip支援J2MEAndroidJava
- WinZip Pro 9 for Mac 專業zip壓縮解壓工具Mac
- 壓縮檔案格式rar和zip有什麼區別 zip和rar哪個是無失真壓縮
- java 壓縮包 遍歷解壓 zip 和 7z 指定格式檔案Java
- php 建立壓縮包zip,並將指定檔案放入zip中PHP
- 壓縮檔案格式rar和zip有什麼區別 壓縮檔案格式rar和zip哪個好
- The Unarchiver - Unzip RAR ZIP Mac - mac解壓縮工具HiveMac
- flowable 從zip壓縮包 部署流程定義
- Linux tar 打包 gz bz xz zip 壓縮Linux
- Go 建立帶密碼的zip壓縮包Go密碼
- p7zip 解壓超過 4G壓縮包