扩展名为.Z的压缩文件为LINUX系统下的压缩文件类型,它有独立的算法.我想求助各位大虾,教我如何在Java中解压缩扩展名为.Z的压缩文件,如有操作.Z文件类型的包(比如:java.util.zip.*;是操作ZIP类型的压缩文件的包)更好.

解决方案 »

  1.   

    给你一个我自己刚写的方法:
    一次性解压
       * <p>缺陷:不能解压zip文件中存在中文的文件</p>
       * @param astrZipPath String --> 解压缩后文件存放的路径
       * @param astrZipFile String --> 需要解压缩的zip文件    public void UnZipI(String astrZipPath, String astrZipFile) {
            ZipInputStream ZipOut = null; // 利用Zip输入流来进行读取文件,进行解压
            ZipEntry zEntry = null; // Zip文件中的压缩文件实体
            try {
                // 读文件
                ZipOut = new ZipInputStream(new FileInputStream(astrZipFile));
                while ((zEntry = ZipOut.getNextEntry()) != null) {
                    // 检测文件是否存在不存在则创建,进入下一循环
                    if (zEntry.isDirectory()) {
                        File dir = new File(astrZipPath + zEntry.getName().toString());
                        if (!dir.exists()) {
                            dir.mkdir();
                        }
                        continue;
                    }
                    System.out.println("Extracting... " + zEntry);
                    // 创建与zip文件中相同的文件
                    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(astrZipPath +
                            Utf8toGbk(zEntry.getName()))));
                    String strSize = String.valueOf(zEntry.getSize());
                    int iSize = 1024;
                    iSize = Integer.parseInt(strSize);
                    byte[] buf = new byte[iSize];
                    // 将zip文件中的数据写出
                    while (ZipOut.read(buf, 0, buf.length) != -1) {
                        out.write(buf, 0, buf.length);
                    }
                    ZipOut.closeEntry();
                    out.close();
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    ZipOut.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }该解压方式是利用ZipInputStream进行解压,还有别的方式可以解压
      

  2.   

    yoyozo!谢谢您的帮助.您的程序我已测试过,现在有两个问题.
    1.Utf8toGbk()这个方法,是系统的还是自己创建的?
    2.// 读文件
      ZipOut = new ZipInputStream(new FileInputStream(astrZipFile));
      读不到.Z类型的文件.ZipOut.getNextEntry()=NULL;
      

  3.   

    不好意思:Utf8toGbk()方法如下:
        public static String Utf8toGbk(String str) {        if (str == null) {
                str = "";
            }
            String strTmp = null;
            try {
                strTmp = new String(str.getBytes("ISO-8859-1"), "GBK");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
                strTmp = "";
            }
            //return new String(str.getBytes("ISO-8859-1"), "GBK");
            return strTmp;
        }
    2/.Z类型的文件是压缩文件吗,类ZipInputStream是专门提供给压缩文件读取的.
      

  4.   

    扩展名为.Z的压缩文件为LINUX系统下的压缩文件类型,它是由compress压缩的文件.而ZipInputStream好象读不出来这种压缩文件.