- 相關(guān)推薦
Java文件解壓縮示例
新一批的考生已經(jīng)開始備戰(zhàn)2016年JAVA認(rèn)證考試。為了幫助廣大考試新手們更加順利地復(fù)習(xí)備考,小編整理了JAVA認(rèn)證復(fù)習(xí)備考資料,希望能夠?qū)Υ蠹矣兴鶐椭?/p>
Java實現(xiàn)壓縮文件的解壓縮操作,缺點是壓縮文件內(nèi)不能含有文件名為中文的的文件,否則會出現(xiàn)如下錯誤:
Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED
at java.util.zip.ZipCoder.toString(Unknown Source)
at java.util.zip.ZipInputStream.readLOC(Unknown Source)
at java.util.zip.ZipInputStream.getNextEntry(Unknown Source)
at com.javatest.techzero.gui.ZipFileDemo.main(ZipFileDemo.java:22)
代碼:
package com.javatest.techzero.gui;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ZipFileDemo {
@SuppressWarnings("resource")
public static void main(String args[]) throws Exception {
File file = new File("d:" + File.separator + "test.zip");
File outFile = null;
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
InputStream input = null;
OutputStream out = null;
while ((entry = zipInput.getNextEntry()) != null) {
System.out.println("開始解壓縮" + entry.getName() + "文件。。。");
outFile = new File("d:" + File.separator + entry.getName());
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdir();
}
if (!outFile.exists()) {
outFile.createNewFile();
}
input = zipFile.getInputStream(entry);
out = new FileOutputStream(outFile);
int temp = 0;
while ((temp = input.read()) != -1) {
//System.out.println(temp);
out.write(temp);
}
input.close();
out.close();
}
System.out.println("Done!");
}
}
【Java文件解壓縮示例】相關(guān)文章:
Java文件解壓縮實例詳解201603-04
監(jiān)理文件資料的類型-監(jiān)理文件資料分類03-27
什么是DOS文件03-20
學(xué)習(xí)Java的技巧03-05
java習(xí)題及答案03-26
JAVA認(rèn)證簡介03-19
關(guān)于Java的import03-16