zoukankan      html  css  js  c++  java
  • ZipFile和ZipInputSteam解压zip文件

    最近有个需求,要接受上穿的zip文件,解压后读取里面的文件(应该还有目录),提前储备一下需要的知识。

    贴在博客上,有需要的可以参考。

    ZipInputStream解压文件:

    @Test
    public void test() {
    String fileName = "/attach/01.zip";
    String dest = "/attach/22";
    try {
    getFileInZip(fileName, dest);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private void getFileInZip(String fileName, String dest) throws IOException {

    File file = new File(fileName);
    ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
    OutputStream out = null;
    ZipEntry zipEntry = null;
    while ((zipEntry = zis.getNextEntry()) != null) {
    File temp = new File(dest + File.separator + zipEntry.getName());
    if (zipEntry.isDirectory()) {
    temp.mkdirs();
    continue;
    }
    System.out.println(zipEntry.getName() + "---" + zipEntry.getSize());
    out = new FileOutputStream(temp);
    int len = 0;
    byte[] bytes = new byte[1024];
    while ((len = zis.read(bytes)) != -1) {
    out.write(bytes, 0, len);
    }
    out.close();
    zis.closeEntry();
    }
    zis.close();
    }

    ZipFile解压文件:

    @Test
    public void test2() {
    String fileName = "/attach/01.zip";
    String dest = "/attach/22";
    try {
    testZipFile(fileName, dest);
    } catch (ZipException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private void testZipFile(String fileName, String dest) throws ZipException, IOException {
    File file = new File(fileName);
    ZipFile zipFile = new ZipFile(file);
    Enumeration entries = zipFile.entries();
    InputStream inputStream = null;
    while (entries.hasMoreElements()) {

    ZipEntry zipEntry = (ZipEntry) entries.nextElement();
    File temp = new File(dest + File.separator + zipEntry.getName());
    if (zipEntry.isDirectory()) {
    temp.mkdirs();
    continue;
    }
    inputStream = zipFile.getInputStream(zipEntry);

    System.out.println(zipEntry.getName() + "---" + zipEntry.getSize());

    if (!temp.getParentFile().exists()) {
    temp.getParentFile().mkdirs();

    }
    FileOutputStream outputStream = new FileOutputStream(temp);
    int len = 0;
    byte bytes[] = new byte[1024];
    while ((len = inputStream.read(bytes)) != -1) {
    outputStream.write(bytes, 0, len);
    }
    outputStream.close();
    }

    }

    注:关于路径的说明,可能有的朋友会以为"/attach/01.zip”是linux或是unix里的路径,

      其实是windows里的路径(linux或unix里的路径就是这样的),我的eclipse是放在D盘的,

      使用"/attach/01.zip"会直接定位到“D:\attach\01.zip”

      windows的路径分隔符是 “”,在java中是“”是转义字符,所以需要写成"\"

      linux的路径分隔符是 “/”,就不需要转义了

      当然在windows中使用相对路径时,是可以用“/”作为分隔符的,如:"/attach/01.zip”

  • 相关阅读:
    【kafka学习之五】kafka运维:kafka操作日志设置和主题删除
    【Redis学习之十一】Java客户端实现redis集群操作
    FastDFS:Java客户都实现文件的上传、下载、修改、删除
    DevOps之四:Git & GitLab
    代码静态检查Eclipse插件:SonarLint插件离线安装
    DevOps之三:CentOS7.3 安装部署Jenkins(三种方式) & Hudson
    DevOps之三:Maven私服Nexus使用 && 清理nexus历史镜像
    DevOps之三:搭建Maven私服Nexus
    DevOps之二:搭建SVN服务器(SvnAdmin)
    财务对账
  • 原文地址:https://www.cnblogs.com/Springmoon-venn/p/5483833.html
Copyright © 2011-2022 走看看