用任何语言实现解压缩功能,谁有类似的例子,或有关的内容也可以,希望大家帮帮忙!救急啊。。就是压缩成zip,最好有源码,再次拜谢!!!!很着急的!!!

解决方案 »

  1.   

    File file = new File("c:/test/a.txt");
    byte[] file1Content = new byte[(int) file.length()];
    FileInputStream in = new FileInputStream(file);
    in.read(file1Content);
    in.close(); file = new File("c:/test/b.txt");
    byte[] file2Content = new byte[(int) file.length()];
    in = new FileInputStream(file);
    in.read(file2Content);
    in.close(); file = new File("c:/test/c/c.txt");
    byte[] file3Content = new byte[(int) file.length()];
    in = new FileInputStream(file);
    in.read(file3Content);
    in.close(); // ========== 写到一个 zip 中
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream("c:/test/test.zip")); out.putNextEntry(new ZipEntry("a.txt"));
    out.write(file1Content);
    out.closeEntry(); out.putNextEntry(new ZipEntry("b.txt"));
    out.write(file2Content);
    out.closeEntry(); // 指定文件夹的相对路径(相对于zip包中的根目录),并以'/'结尾,会递归的创建指定的文件夹
    out.putNextEntry(new ZipEntry("c/c1/"));
    out.closeEntry(); out.putNextEntry(new ZipEntry("c/c.txt"));
    out.write(file3Content);
    out.closeEntry(); out.close(); // 一定要关闭
    System.out.println("end");jdk自带的zipOutStream不足之处是文件夹和文件名不能为中文,
    也可以使用apache提交的jar包,可以设置编码的能解决中文问题
      

  2.   

    用到了ant.jar来解决压缩中的中文乱码问题。
    package unzip;
     
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Enumeration;
     
    import org.apache.tools.zip.ZipEntry;
    import org.apache.tools.zip.ZipFile;
    import org.apache.tools.zip.ZipOutputStream;
     
    /**
     * 功能: 1、实现把指定文件夹下的所有文件压缩为指定文件夹下指定zip文件 2、实现把指定文件夹下的zip文件解压到指定目录下
     *
     * @author ffshi
     *
     */
    public class ZipUtils {
     
        public static void main(String[] args) {
     
           // 把E盘正则表达式文件夹下的所有文件压缩到E盘stu目录下,压缩后的文件名保存为 正则表达式.zip
           // zip("E:\\正则表达式", "E:\\stu\\正则表达式.zip");
           // 把E盘stu目录下的正则表达式.zip压缩文件内的所有文件解压到E盘stu目录下面
           unZip("E:\\stu\\正则表达式.zip", "E:\\stu");
     
        }
     
        /**
         * 功能:把sourceDir目录下的所有文件进行zip格式的压缩,保存为指定zip文件 create date:2009-6-9
         * author:Administrator
         *
         * @param sourceDir
         *            E:\\我的备份
         * @param zipFile
         *            格式:E:\\stu\\zipFile.zip 注意:加入zipFile我们传入的字符串值是
         *            :"E:\\stu\\"或者"E:\\stu"
         *            如果E盘已经存在stu这个文件夹的话,那么就会出现java.io.FileNotFoundException: E:\stu
         *            (拒绝访问。)这个异常,所以要注意正确传参调用本函数哦
         *
         */
        public static void zip(String sourceDir, String zipFile) {
           OutputStream os;
           try {
               os = new FileOutputStream(zipFile);
               BufferedOutputStream bos = new BufferedOutputStream(os);
               ZipOutputStream zos = new ZipOutputStream(bos);
     
               File file = new File(sourceDir);
     
               String basePath = null;
               if (file.isDirectory()) {
                  basePath = file.getPath();
               } else {
                  basePath = file.getParent();
               }
     
               zipFile(file, basePath, zos);
     
               zos.closeEntry();
               zos.close();
           } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
     
        }
     
        /**
         *
         * create date:2009-6-9 author:Administrator
         *
         * @param source
         * @param basePath
         * @param zos
         * @throws IOException
         */
        private static void zipFile(File source, String basePath,
               ZipOutputStream zos) {
           File[] files = new File[0];
     
           if (source.isDirectory()) {
               files = source.listFiles();
           } else {
               files = new File[1];
               files[0] = source;
           }
     
           String pathName;
           byte[] buf = new byte[1024];
           int length = 0;
           try {
               for (File file : files) {
                  if (file.isDirectory()) {
                      pathName = file.getPath().substring(basePath.length() + 1)
                             + "/";
                      zos.putNextEntry(new ZipEntry(pathName));
                      zipFile(file, basePath, zos);
                  } else {
                      pathName = file.getPath().substring(basePath.length() + 1);
                      InputStream is = new FileInputStream(file);
                      BufferedInputStream bis = new BufferedInputStream(is);
                      zos.putNextEntry(new ZipEntry(pathName));
                      while ((length = bis.read(buf)) > 0) {
                         zos.write(buf, 0, length);
                      }
                      is.close();
                  }
               }
           } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
     
        }
     
        /**
         * 解压zip文件,注意不能解压rar文件哦,只能解压zip文件 解压rar文件 会出现java.io.IOException: Negative
         * seek offset异常 create date:2009-6-9 author:Administrator
         *
         * @param zipfile
         *            zip文件,注意要是正宗的zip文件哦,不能是把rar的直接改为zip这样会出现java.io.IOException:
         *            Negative seek offset异常
         * @param destDir
         * @throws IOException
         */
        public static void unZip(String zipfile, String destDir) {
     
           destDir = destDir.endsWith("\\") ? destDir : destDir + "\\";
           byte b[] = new byte[1024];
           int length;
     
           ZipFile zipFile;
           try {
               zipFile = new ZipFile(new File(zipfile));
               Enumeration enumeration = zipFile.getEntries();
               ZipEntry zipEntry = null;
     
               while (enumeration.hasMoreElements()) {
                  zipEntry = (ZipEntry) enumeration.nextElement();
                  File loadFile = new File(destDir + zipEntry.getName());
     
                  if (zipEntry.isDirectory()) {
                      // 这段都可以不要,因为每次都貌似从最底层开始遍历的
                      loadFile.mkdirs();
                  } else {
                      if (!loadFile.getParentFile().exists())
                         loadFile.getParentFile().mkdirs();
     
                      OutputStream outputStream = new FileOutputStream(loadFile);
                      InputStream inputStream = zipFile.getInputStream(zipEntry);
     
                      while ((length = inputStream.read(b)) > 0)
                         outputStream.write(b, 0, length);
     
                  }
               }
               System.out.println("文件解压成功");
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
     
        }
     
    }