您的位置:首页 > 编程语言 > Java开发

java不解压zip文件时,获取其中的文件

2015-11-02 11:15 459 查看
直接上代码

public static void unzip(InputStream ins, String targetFile) {

BufferedOutputStream dest = null;

ZipEntry entry;

ZipInputStream zin = null;

FileOutputStream fos = null;

try {

zin = new ZipInputStream(new BufferedInputStream(ins));

while ((entry = zin.getNextEntry()) != null) {

//判断是否是目录

if(!entry.isDirectory()){

System.out.println("Extracting: " + entry);

int count;

byte data[] = new byte[BUFFER];

String filePath = targetFile + "/" + entry.getName();

String path = filePath.substring(0, filePath.lastIndexOf("/"));

File dir = new File(path);

if (!dir.exists()) {

dir.mkdirs();

}

System.out.println("图片名称=entry===" + entry);

System.out.println("图片名称=entry.getName()===" + entry.getName());

System.out.println("图片名称=entry.getName().toLowerCase()===" + entry.getName().toLowerCase());

// fos = new FileOutputStream(targetFile + "/"

// + entry.getName().toLowerCase());

String picName = entry.getName().substring(entry.getName().indexOf("/")+1);

System.out.println("图片名称=picName===" + picName);

fos = new FileOutputStream(targetFile + "/" + picName);

dest = new BufferedOutputStream(fos, BUFFER);

while ((count = zin.read(data, 0, BUFFER)) != -1) {

dest.write(data, 0, count);

}

dest.flush();

dest.close();

}

}

zin.close();

fos.flush();

fos.close();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (dest != null) {

dest.close();

}

if (zin != null) {

zin.close();

}

if (fos != null) {

fos.close();

}

} catch (Exception e) {

}

}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: