這個(gè)程序是沒(méi)有任何問(wèn)題的,就是你創(chuàng)建文件路徑的時(shí)候估計(jì)沒(méi)創(chuàng)建好,從你的程序中可以看到,首先創(chuàng)建一個(gè)輸出流ZipOutputStream?對(duì)象,然后創(chuàng)建一個(gè)輸入流對(duì)象FileInputStream,通過(guò)輸入流讀取文件testfile.txt的內(nèi)容,然后把這個(gè)內(nèi)容通過(guò)輸出流輸出到壓縮文件testfile.zip。

成都創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、合浦網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、購(gòu)物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為合浦等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
根據(jù)你的程序,首先要在E盤(pán)下創(chuàng)建這樣一個(gè)路徑,E:\java\java?works\testfile,然后在文件夾testfile下面創(chuàng)建文件testfile.txt,這個(gè)時(shí)候你可以在testfile.txt文件中寫(xiě)上一些內(nèi)容,因?yàn)槟闶菑倪@個(gè)文件讀取內(nèi)容的,如果你事先不創(chuàng)建這個(gè)文件的話,那么就會(huì)拋出找不到文件的異常。
我新建的文件路徑如下圖所示:
運(yùn)行程序之后,多了一個(gè)壓縮文件,解壓testfile.zip文件,里面有testfile.txt文件培薯冊(cè),并且內(nèi)容是我剛剛輸入的內(nèi)容,壓縮成功:
你的程序我做了一點(diǎn)小的改動(dòng),代碼如下:
package?life;
import?java.io.*;
//import?java.util.*;//這個(gè)包不需要。
import?java.util.zip.*;
public?class?practice?{
public?static?void?main(String[]?args)?throws?Exception?{
File?myfile?=?new?File("e:/java/java?works/testfile/testfile.txt");//創(chuàng)建一個(gè)File對(duì)象。
File?newfile?=?new?File("e:/java/java?works/testfile/testfile.zip");//創(chuàng)建一個(gè)File對(duì)象。
ZipOutputStream?zip?=?new?ZipOutputStream(new?FileOutputStream(newfile));//?以輸出流來(lái)創(chuàng)建ZipOutputStream對(duì)象。
String?e?=?"testfile.txt";//這里我改了一下,指定壓縮文件之后壓縮包所包含的文件的名稱和文件格式,如果是配宏寫(xiě)String?e=""的話,壓縮成功之后,壓縮包里面的文件是一個(gè)不可識(shí)別的文件,文件名為testfile但是沒(méi)有后綴名.txt。
zip.putNextEntry(new?ZipEntry(e));
System.out.println(e);//打印壓縮包里面文件的名稱。
FileInputStream?手孝in?=?new?FileInputStream(myfile);//以字節(jié)流從文件testfile.txt讀取內(nèi)容。
int?i?=?0;
while?((i?=?in.read())?!=?-1)?{
zip.write(i);//把讀取到的內(nèi)容寫(xiě)到壓縮文件,壓縮文件名稱為testfile.zip。
}
zip.close();
in.close();
}
}
通過(guò)這個(gè)程序我又學(xué)到了新的東西,受益匪淺。
直接通過(guò)工具類進(jìn)行解壓或者壓縮文件即可。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* @author gdb
*/
public class ZipUtilAll {
public static final int DEFAULT_BUFSIZE = 1024 * 16;
/**
* 解壓Zip文件簡(jiǎn)態(tài)
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(File srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解壓Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(String srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解壓Zip文件
*
* @param zipFile
* @param destDir
* @throws IOException
*/
public static void unZip(ZipFile zipFile, String destDir) throws IOException
{
Enumeration? extends ZipEntry entryEnum = zipFile.entries();
ZipEntry entry = null;
while (entryEnum.hasMoreElements()) {
entry = entryEnum.nextElement();
File destFile = new File(destDir + entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
}
else {
destFile.getParentFile().mkdirs();
InputStream eis = zipFile.getInputStream(entry);
System.out.println(eis.read());
write(eis, destFile);
}
}
}
/**
* 將輸入流中的數(shù)清咐大據(jù)寫(xiě)到指定文答豎件
*
* @param inputStream
* @param destFile
*/
public static void write(InputStream inputStream, File destFile) throws IOException
{
BufferedInputStream bufIs = null;
BufferedOutputStream bufOs = null;
try {
bufIs = new BufferedInputStream(inputStream);
bufOs = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[DEFAULT_BUFSIZE];
int len = 0;
while ((len = bufIs.read(buf, 0, buf.length)) 0) {
bufOs.write(buf, 0, len);
}
} catch (IOException ex) {
throw ex;
} finally {
close(bufOs, bufIs);
}
}
/**
* 安全關(guān)閉多個(gè)流
*
* @param streams
*/
public static void close(Closeable... streams)
{
try {
for (Closeable s : streams) {
if (s != null)
s.close();
}
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");
unZip("D:/123/123.zip", "D:/123/");
// new File();
}
}
分享標(biāo)題:java上傳壓縮包代碼 java上傳zip壓縮文件
文章起源:http://chinadenli.net/article39/dsppsph.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)頁(yè)設(shè)計(jì)公司、、用戶體驗(yàn)、標(biāo)簽優(yōu)化、ChatGPT
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)