給你一段單個文件解壓gzip文件代碼
10年積累的成都做網站、網站建設經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先做網站后付款的網站建設流程,更有河源免費網站建設讓你可以放心的選擇與我們合作。
批量解壓的話 File f = new File("要解壓的文件夾目錄");
String paths[] = f.list(); // 取得文件夾下的文件
然后循環(huán)調用下面的方法就可以了。
try {
// Open the compressed file
String inFilename = "infile.gzip";
GZIPInputStream in = new GZIPInputStream(new FileInputStream(inFilename));
// Open the output file
String outFilename = "outfile";
OutputStream out = new FileOutputStream(outFilename);
// Transfer bytes from the compressed file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) 0) {
out.write(buf, 0, len);
}
// Close the file and stream
in.close();
out.close();
} catch (IOException e) {
}
import java.io.*;
import java.util.zip.*;
public class GZIPcompress {
public static void main(String[] args) {
try {
BufferedReader in =
new BufferedReader(
new FileReader(args[0]));
BufferedOutputStream out =
new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream("test.gz")));
System.out.println("Writing file");
int c;
while((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
System.out.println("Reading file");
BufferedReader in2 =
new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
new FileInputStream("test.gz"))));
String s;
while((s = in2.readLine()) != null)
System.out.println(s);
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
最后怎么解決的,我現在也遇到這個問題了,單個文件可以解壓可以壓縮,寫入的測試內容也在,換成文件夾就不行了。能找到的案例全都是解壓成文件,但是本身是個文件夾的GZ包解壓了以后也打不開。
1.
將文件壓縮為 .gz 格式,只能壓縮文件:gzip ①、命令名稱:gzip ②、英文原意:GNU zip ③、命令所在路徑:/bin/gzip ④、執(zhí)行權限:所有用戶 ...
2.
將 .gz 文件解壓:gunzip
命令名稱:gunzip
在JDK中有一個zip工具類:
java.util.zip ? ?Provides classes for reading and writing the standard ZIP and
GZIP file formats.
使用此類可以將文件夾或者多個文件進行打包壓縮操作。
在使用之前先了解關鍵方法:
ZipEntry(String name) ????????Creates a new zip entry with the specified name.
使用ZipEntry的構造方法可以創(chuàng)建一個zip壓縮文件包的實例,然后通過ZipOutputStream將待壓縮的文件以流的形式寫進該壓縮包中。具體實現代碼如下:
import?java.io.BufferedInputStream;??
import?java.io.BufferedOutputStream;??
import?java.io.File;??
import?java.io.FileInputStream;??
import?java.io.FileNotFoundException;??
import?java.io.FileOutputStream;??
import?java.io.IOException;??
import?java.util.zip.ZipEntry;??
import?java.util.zip.ZipOutputStream;??
/**?
*?將文件夾下面的文件?
*?打包成zip壓縮文件?
*??
*?@author?admin?
*?
*/??
public?final?class?FileToZip?{??
private?FileToZip(){}??
/**?
*?將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,并存放到zipFilePath路徑下?
*?@param?sourceFilePath?:待壓縮的文件路徑?
*?@param?zipFilePath?:壓縮后存放路徑?
*?@param?fileName?:壓縮后文件的名稱?
*?@return?
*/??
public?static?boolean?fileToZip(String?sourceFilePath,String?zipFilePath,String?fileName){??
boolean?flag?=?false;??
File?sourceFile?=?new?File(sourceFilePath);??
FileInputStream?fis?=?null;??
BufferedInputStream?bis?=?null;??
FileOutputStream?fos?=?null;??
ZipOutputStream?zos?=?null;??
if(sourceFile.exists()?==?false){??
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"不存在.");??
}else{??
try?{??
File?zipFile?=?new?File(zipFilePath?+?"/"?+?fileName?+".zip");??
if(zipFile.exists()){??
System.out.println(zipFilePath?+?"目錄下存在名字為:"?+?fileName?+".zip"?+"打包文件.");??
}else{??
File[]?sourceFiles?=?sourceFile.listFiles();??
if(null?==?sourceFiles?||?sourceFiles.length1){??
System.out.println("待壓縮的文件目錄:"?+?sourceFilePath?+?"里面不存在文件,無需壓縮.");??
}else{??
fos?=?new?FileOutputStream(zipFile);??
zos?=?new?ZipOutputStream(new?BufferedOutputStream(fos));??
byte[]?bufs?=?new?byte[1024*10];??
for(int?i=0;isourceFiles.length;i++){??
//創(chuàng)建ZIP實體,并添加進壓縮包??
ZipEntry?zipEntry?=?new?ZipEntry(sourceFiles[i].getName());??
zos.putNextEntry(zipEntry);??
//讀取待壓縮的文件并寫進壓縮包里??
fis?=?new?FileInputStream(sourceFiles[i]);??
bis?=?new?BufferedInputStream(fis,?1024*10);??
int?read?=?0;??
while((read=bis.read(bufs,?0,?1024*10))?!=?-1){??
zos.write(bufs,0,read);??
}??
}??
flag?=?true;??
}??
}??
}?catch?(FileNotFoundException?e)?{??
e.printStackTrace();??
throw?new?RuntimeException(e);??
}?catch?(IOException?e)?{??
e.printStackTrace();??
throw?new?RuntimeException(e);??
}?finally{??
//關閉流??
try?{??
if(null?!=?bis)?bis.close();??
if(null?!=?zos)?zos.close();??
}?catch?(IOException?e)?{??
e.printStackTrace();??
throw?new?RuntimeException(e);??
}??
}??
}??
return?flag;??
}??
public?static?void?main(String[]?args){??
String?sourceFilePath?=?"D:\\TestFile";??
String?zipFilePath?=?"D:\\tmp";??
String?fileName?=?"12700153file";??
boolean?flag?=?FileToZip.fileToZip(sourceFilePath,?zipFilePath,?fileName);??
if(flag){??
System.out.println("文件打包成功!");??
}else{??
System.out.println("文件打包失敗!");??
}??
}??
}
當前名稱:java壓縮文件gz代碼 java解壓gz
當前路徑:http://chinadenli.net/article16/doddjdg.html
成都網站建設公司_創(chuàng)新互聯,為您提供網頁設計公司、外貿網站建設、微信小程序、小程序開發(fā)、Google、網站維護
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯