看了API中关于ZIPINPUTSTREAM的例子,照着输入后,运行出错,
import java.util.zip.*;
import java.io.*;
public class ZipTest{
private static final int BUFFER=2048;
public static void main(String [] args){
try{
ZipInputStream zis=new ZipInputStream(new DataInputStream(new FileInputStream("D:/JAVa/Zip/testZip.zip")));
ZipEntry entry; String path="D:/JAVa/Zip/";[ 18,] while( (entry=zis.getNextEntry())!=null){
System.out.println("ENTRY"+entry);
int count;
byte [] data=new byte[BUFFER];
FileOutputStream fos=new FileOutputStream(path+entry.getName());
ZipOutputStream dest=null;
dest=new ZipOutputStream(new BufferedOutputStream(fos,BUFFER));
while( (count=zis.read(data,0,BUFFER))!=-1){
dest.write(data,0,count);
}
dest.flush();
dest.close();
}
zis.close();
}catch(IOException e){
e.printStackTrace();}
}
}
Exception in thread "main" java.lang.IllegalArgumentException
at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:293)
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:247)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:74)
at zip.ZipTest.main(ZipTest.java:18)
18行是 while( (entry=zis.getNextEntry())!=null){非法参数异常,想不通额,难道是路径有错吗 N 希望有人指点下
import java.util.zip.*;
import java.io.*;
public class ZipTest{
private static final int BUFFER=2048;
public static void main(String [] args){
try{
ZipInputStream zis=new ZipInputStream(new DataInputStream(new FileInputStream("D:/JAVa/Zip/testZip.zip")));
ZipEntry entry; String path="D:/JAVa/Zip/";[ 18,] while( (entry=zis.getNextEntry())!=null){
System.out.println("ENTRY"+entry);
int count;
byte [] data=new byte[BUFFER];
FileOutputStream fos=new FileOutputStream(path+entry.getName());
ZipOutputStream dest=null;
dest=new ZipOutputStream(new BufferedOutputStream(fos,BUFFER));
while( (count=zis.read(data,0,BUFFER))!=-1){
dest.write(data,0,count);
}
dest.flush();
dest.close();
}
zis.close();
}catch(IOException e){
e.printStackTrace();}
}
}
Exception in thread "main" java.lang.IllegalArgumentException
at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:293)
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:247)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:74)
at zip.ZipTest.main(ZipTest.java:18)
18行是 while( (entry=zis.getNextEntry())!=null){非法参数异常,想不通额,难道是路径有错吗 N 希望有人指点下
String zipname = "C:/b.zip";
ZipFile zipFile = new ZipFile(zipname);
Enumeration enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
System.out.println("Unzipping: " + zipEntry.getName());
BufferedInputStream bis = new BufferedInputStream(zipFile
.getInputStream(zipEntry));
int size;
byte[] buffer = new byte[2048];
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(zipEntry.getName()), buffer.length);
while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
bis.close();
}
}
/**
* 解压 zip 文件
*
* @param zipFile
* 待解压的 zip 文件
*
* @param path
* 解压的目的地
*
* @throws IOException
*/
private static void unZip(File zipFile, String path) throws IOException {
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry entry = null; // 创建解压目录
File unzipDirectory = new File(path);
unzipDirectory.mkdir(); while ((entry = zin.getNextEntry()) != null) {
if (entry.isDirectory()) {
// 如果是目录,则创建目录
File dataDirectory = new File(path, entry.getName());
dataDirectory.mkdir();
zin.closeEntry();
} else {
// 如果是文件,则创建文件
FileOutputStream fout = new FileOutputStream(path + "\\"
+ entry.getName());
DataOutputStream dout = new DataOutputStream(fout);
byte[] b = new byte[1024];
int len = 0;
while ((len = zin.read(b)) != -1) {
dout.write(b, 0, len);
}
dout.close();
fout.close();
zin.closeEntry();
}
} if (zin != null) {
zin.close();
}
}