Zip文件的内容

public static void testZipToUnzip(ZipFile zipfile,File file){
  //将一个压缩文件zipfile(可以包含文件)解压到file文件中
  ZipEntry ze=null;
  File outfile;
  InputStream zis=null;
  FileOutputStream fos=null;
  Enumeration e=zipfile.getEntries();
  while(e.hasMoreElements()){
   ze=(ZipEntry) e.nextElement();
      // System.out.println(ze.getName());
   outfile=new File(file+File.separator+ze.getName());   
   //if(ze.toString().length()!=(ze.getName()).lastIndexOf('/')+1)
   //这句话的作用:如果ze不是一个目录则执行下面的语句,也就是创建输出文件并且将输入流输出到输出流中。
   //上面的if语句作用和该语句作用一样
   if(!ze.isDirectory()){         
    try {
     //System.out.println(ze.getName());
     zis=zipfile.getInputStream(ze);
     //System.out.println(ze.getSize());    
     //System.out.println(outfile.getPath());     
     if(!outfile.exists())
      //如果输出文件不存在,那么创建输出文件的父目录
      outfile.getParentFile().mkdirs();    
 fos=new FileOutputStream(outfile);     
     byte[] b=new byte[1024];
     int len;
     while((len=zis.read(b))>0){
      fos.write(b, 0, len);
     }
     fos.close();
     zis.close();
    } catch (ZipException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在macOS上,可以使用`QProcess`执行`unzip`命令来将zip文件内容解压到指定目录。例如,以下代码将解压名为`myzipfile.zip`的压缩文件到`/Users/username/Documents`目录: ```cpp QProcess process; process.setWorkingDirectory("/Users/username/Documents"); // 设置工作目录 process.start("unzip", QStringList() << "-q" << "/path/to/myzipfile.zip"); process.waitForFinished(); ``` 在这个例子中,`setWorkingDirectory()`函数设置了`QProcess`的工作目录,即将解压到的目录。`unzip`是要执行的命令,`-q`是安静模式选项,不输出任何信息,`/path/to/myzipfile.zip`是要解压的文件路径。`QProcess`会启动一个新的进程来执行这个命令,并等待进程执行完成。 需要注意的是,解压到的目录需要有足够的权限让程序写入文件。否则,`unzip`命令可能会失败。 另外,如果要解压其他类型的压缩文件,可以将选项参数替换为相应的选项。例如,以下代码将解压名为`mytarfile.tar.gz`的tar文件到`/Users/username/Documents`目录: ```cpp QProcess process; process.setWorkingDirectory("/Users/username/Documents"); // 设置工作目录 process.start("tar", QStringList() << "-zxvf" << "/path/to/mytarfile.tar.gz"); process.waitForFinished(); ``` 在这个例子中,`tar`是要执行的命令,`-zxvf`是解压选项,`/path/to/mytarfile.tar.gz`是要解压的文件路径。`-C`选项不再需要,因为`setWorkingDirectory()`已经设置了解压到的目录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值