package com.lch.test;
發(fā)展壯大離不開廣大客戶長期以來的信賴與支持,我們將始終秉承“誠信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠服務(wù)每家企業(yè),認(rèn)真做好每個(gè)細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及銅雕雕塑等,在成都網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZIP {
public static void main(String[] argv) throws Exception {
ZipFile zf = new ZipFile("E:\\wk\\LBSLEMIS201106141057\\LBSLEMIS\\test\\com\\lch\\test\\filename.zip");
for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
String zipEntryName = ((ZipEntry) entries.nextElement()).getName();
System.out.println(zipEntryName);
}
}
}
用javad 的ZipFile類的ZipEntry方法試一下 找到ZIP里面的ZipEntry方法 讀取Zip里面壓縮文件的內(nèi)容
有可能會引用外包
你好,我不知道你說的dzp是什么格式文件,但如果是zip的壓縮文件,可以看下我的這段代碼
ZipFile file = new ZipFile("d:\\1.zip");
ZipEntry entry = file.getEntry("1.xml"); //假如壓縮包里的文件名是1.xml
InputStream in=file.getInputStream(entry);
最后就是按照java中一貫的流的處理方式即可
可以不解壓,zip包里的一個(gè)對象就是一個(gè)ZipEntry
找到你想要的那個(gè)ZipEntry,用文流寫出來就可以了。追問通過ZipEntry,然后用流就可以讀出里面的內(nèi)容了嗎?謝謝指點(diǎn)!
回答/**
* 解壓
* @param root 輸出目標(biāo)
* @param zipfile zip文件
*/
protected void unzip(File root, File zipfile, String file) throws Exception {
// 解壓文件不存在時(shí)返回
if (!zipfile.exists()) {
return;
}
// 釋放目錄不存時(shí)創(chuàng)建
if (!root.exists()) {
root.mkdirs();
}
// 釋放目錄不為目錄時(shí)返回
if (!root.isDirectory()) {
return;
}
FileInputStream fin = new FileInputStream(zipfile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry entry = null;
while ((entry = zin.getNextEntry()) != null) {
// if (!entry.getName().endsWith(file)) {
// continue;
// }
File tmp = new File(root, entry.getName());
if (entry.isDirectory()) {
tmp.mkdirs();
} else {
byte[] buff = new byte[4096];
int len = 0;
tmp.getParentFile().mkdirs();
FileOutputStream fout = new FileOutputStream(tmp);
while ((len = zin.read(buff)) != -1) {
fout.write(buff, 0, len);
}
zin.closeEntry();
fout.close();
}
}
}
這里完整的解壓代碼。
// if (!entry.getName().endsWith(file)) {
// continue;
// }
這段打開就是只解出一個(gè)你指定的文件。
下面是測試用的。
public static void main(String[] args) throws Exception {
new CommonFiles().unzip(new File("D:\\"), new File("D:\\test.zip"),"file.txt");
}
這個(gè)例子會在D盤生成型個(gè)test文件夾,file.txt就會在里面,(里面也可能會有多個(gè)文件夾,這個(gè)取決于壓縮包里文件的度)
在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("文件打包失敗!");??
}??
}??
}
用java代碼壓縮應(yīng)用到程序了,代碼一般是比較復(fù)雜的,對pdf文件的mate標(biāo)簽優(yōu)化,這類標(biāo)簽包括三類,pdf文件不是網(wǎng)頁就是個(gè)文件,何況我們可以用pdf壓縮工具壓縮,下面有個(gè)解決方法,樓主可以做參照。
1:點(diǎn)擊打開工具,打開主頁面上有三個(gè)功能進(jìn)行選擇,我們選擇pdf文件壓縮。
2:這這個(gè)頁面中我們選擇pdf文件在這里打開,點(diǎn)擊“添加文件”按鈕將文件添加進(jìn)來。
3:然后在頁面中點(diǎn)擊“開始壓縮”就可以開始壓縮文件了。
4:壓縮完成的文件頁面會顯示已經(jīng)完成。
下面的示例代碼演示如何創(chuàng)建zip壓縮包。
首先需要由需要壓縮的文件創(chuàng)建一個(gè)InputStream對象,然后讀取文件內(nèi)容寫入到ZipOutputStream中。
ZipOutputStream類接受FileOutputStream作為參數(shù)。創(chuàng)建號ZipOutputStream對象后需要?jiǎng)?chuàng)建一個(gè)zip entry,然后寫入。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
*
* @author outofmemory.cn
*/
public class Main {
/**
* Creates a zip file
*/
public void createZipFile() {
try {
String inputFileName = "test.txt";
String zipFileName = "compressed.zip";
//Create input and output streams
FileInputStream inStream = new FileInputStream(inputFileName);
ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(zipFileName));
// Add a zip entry to the output stream
outStream.putNextEntry(new ZipEntry(inputFileName));
byte[] buffer = new byte[1024];
int bytesRead;
//Each chunk of data read from the input stream
//is written to the output stream
while ((bytesRead = inStream.read(buffer)) 0) {
outStream.write(buffer, 0, bytesRead);
}
//Close zip entry and file streams
outStream.closeEntry();
outStream.close();
inStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().createZipFile();
}
當(dāng)前文章:java代碼的壓縮包,java下載壓縮包代碼
地址分享:http://chinadenli.net/article22/dsiccjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、定制開發(fā)、建站公司、外貿(mào)建站、小程序開發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)