JAVA ZipFile 解压zip

JAVA ZipFile 解压zip

本Markdown编辑器使用[StackEdit][6]修改而来,用它写博客,将会带来全新的体验哦:


代码块

int buffSize=204800;
Charset charset=Charset.forName("GBK");
String srcPath="C:\\Users\\Joue\\Desktop\\新建文件夹.zip";
String exPath="C:\\Users\\Joue\\Desktop\\";
ZipFile zipFile=new ZipFile(srcPath,charset);
Enumeration enumeration=(Enumeration)zipFile.entries();
while (enumeration.hasMoreElements()) {
    try {
    ZipEntry zipEntry=enumeration.nextElement();
        if(zipEntry.isDirectory()) {//文件夹
            File file=new File(exPath+zipEntry.getName());
            if (!file.exists()){
                file.mkdir();
            }
        }
        else {
            FileOutputStream fileOutputStream = new FileOutputStream(exPath + zipEntry.getName());
            InputStream inputStream=zipFile.getInputStream(zipEntry);
            int count=0,tinybuff=buffSize;
            if(inputStream.available()//读取流中可读取大小
            }
            byte[] datas=new byte[tinybuff];
            while ((count=inputStream.read(datas,0,tinybuff))!=-1){//遇到文件结尾返回-1 否则返回实际的读数
                fileOutputStream.write(datas,0,count);
                if(inputStream.available()else tinybuff=buffSize;
                datas=new byte[tinybuff];
            }
            fileOutputStream.flush();//刷新缓冲
            fileOutputStream.close();
        }
    }
    catch (Exception e){
        e.printStackTrace();
    }
}
zipFile.close();

你可能感兴趣的:(JAVA)