給你一段單個(gè)文件解壓gzip文件代碼

10年積累的成都做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有河源免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
批量解壓的話 File f = new File("要解壓的文件夾目錄");
String paths[] = f.list(); // 取得文件夾下的文件
然后循環(huán)調(diào)用下面的方法就可以了。
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();
}
}
} ///:~
最后怎么解決的,我現(xiàn)在也遇到這個(gè)問題了,單個(gè)文件可以解壓可以壓縮,寫入的測(cè)試內(nèi)容也在,換成文件夾就不行了。能找到的案例全都是解壓成文件,但是本身是個(gè)文件夾的GZ包解壓了以后也打不開。
1.
將文件壓縮為 .gz 格式,只能壓縮文件:gzip ①、命令名稱:gzip ②、英文原意:GNU zip ③、命令所在路徑:/bin/gzip ④、執(zhí)行權(quán)限:所有用戶 ...
2.
將 .gz 文件解壓:gunzip
命令名稱:gunzip
在JDK中有一個(gè)zip工具類:
java.util.zip ? ?Provides classes for reading and writing the standard ZIP and
GZIP file formats.
使用此類可以將文件夾或者多個(gè)文件進(jìn)行打包壓縮操作。
在使用之前先了解關(guān)鍵方法:
ZipEntry(String name) ????????Creates a new zip entry with the specified name.
使用ZipEntry的構(gòu)造方法可以創(chuàng)建一個(gè)zip壓縮文件包的實(shí)例,然后通過ZipOutputStream將待壓縮的文件以流的形式寫進(jìn)該壓縮包中。具體實(shí)現(xiàn)代碼如下:
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實(shí)體,并添加進(jìn)壓縮包??
ZipEntry?zipEntry?=?new?ZipEntry(sourceFiles[i].getName());??
zos.putNextEntry(zipEntry);??
//讀取待壓縮的文件并寫進(jìn)壓縮包里??
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{??
//關(guān)閉流??
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("文件打包失敗!");??
}??
}??
}
當(dāng)前名稱:java壓縮文件gz代碼 java解壓gz
當(dāng)前路徑:http://www.chinadenli.net/article16/doddjdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)、微信小程序、小程序開發(fā)、Google、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)