java上传.zip的文件并覆盖。

public void uploadFile() throws Exception {
File fileTemp=new File(path);
ZipFile zf = new ZipFile(fileTemp, "GBK");//支持中文
Enumeration enu = zf.getEntries();
while (enu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enu.nextElement();
String name = entry.getName();
//如果解压entry是目录,直接生成目录即可,不用写入,如果是文件,要将文件写入
String path = "c:/On/Bin/" + name;
File file = new File(path);
if (entry.isDirectory()) {
file.mkdirs();
} else {
InputStream is = zf.getInputStream(entry);
byte[] buf1 = new byte[1024];
int len;

if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}else{
file.delete();
}
OutputStream out = new FileOutputStream(file);
while ((len = is.read(buf1)) > 0) {
String buf = new String(buf1, 0, len);
out.write(buf1, 0, len);
}
is.close();
out.close();
}
}
// 关闭流
}

你可能感兴趣的:(java)