我用的是java.util.zip包,
我已经实现了下载压缩(里面是XML文件).
但是压缩完之后java.util.zip把这个XML文件里面的标签和内容顺序给打乱了.有没有办法下载 后并压缩成ZIP文件后,还保持原来的XML顺序和内容....

解决方案 »

  1.   

    我用的是java.util.zip包,
    我已经实现了下载压缩(里面是XML文件).
    但是压缩完之后java.util.zip把这个XML文件里面的标签和内容顺序给打乱了.有没有办法下载 后并压缩成ZIP文件后,还保持原来的XML顺序和内容....
    因为我导入的时候先解压缩这个ZIP文件.但是循序和内容给打乱了.导入就出问题了.
      

  2.   

    解压和压缩应该是严格可逆的,就是说不可能啊。代码给你:
    public class Zip {  public void zip(InputStream in, OutputStream out) throws IOException {
        GZIPOutputStream zip = new GZIPOutputStream(out);
        try {
          byte[] buffer = new byte[1024];
          int bytesRead;
          while ( (bytesRead = in.read(buffer)) != -1) {
            zip.write(buffer, 0, bytesRead);
          }
          zip.finish();
        }
        finally {
          if (zip != null) {
            try {
              zip.close();
            }
            catch (Throwable e) {}
          }
        }
      }  public void zip(File f, File fout) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = new FileInputStream(f);
          out = new FileOutputStream(fout);
          zip(in, out);
          in.close();
          in = null;
          boolean deleted;
          try {
            deleted = f.delete();
          }
          catch (Throwable e) {
            deleted = false;
          }
          if (!deleted)
            f.deleteOnExit();
        }
        catch (IOException e) {
          if (out != null)
            try {
              out.close();
              out = null;
            }
            catch (Throwable ex) {}
          if (fout != null) {
            boolean deleted;
            try {
              deleted = fout.delete();
            }
            catch (Throwable ex) {
              deleted = false;
            }
            if (!deleted)
              fout.deleteOnExit();
          }
        }
        finally {
          if (in != null)
            try {
              in.close();
            }
            catch (Throwable e) {}
          if (out != null)
            try {
              out.close();
            }
            catch (Throwable e) {}
        }
      }  public void zip(File fin, String filename) throws IOException {
        zip(fin, new File(filename));
      }  public void zip(String fin, String fout) throws IOException {
        zip(new File(fin), new File(fout));
      }  public void zip(String fin, File fout) throws IOException {
        zip(new File(fin), fout);
      }  public void zip(File f) throws IOException {
        String path = getDirname(f.getPath());
        String name = getBasename(f.getName());
        File dir = new File(path);
        File fout = new File(dir, name + ".zip");
        zip(f, fout);
      }  public String getDirname(String path) {
        char sep = File.separatorChar;
        int pos1 = path.lastIndexOf(sep);
        int pos2 = path.lastIndexOf('/');
        int pos = pos1;
        if (pos1 < pos2)
          pos = pos2;
        if (pos > 0)
          return path.substring(0, pos);
        return ".";
      }  public String getBasename(String path) {
        char sep = File.separatorChar;
        int pos1 = path.lastIndexOf(sep);
        int pos2 = path.lastIndexOf('/');
        int pos = pos1;
        if (pos1 < pos2)
          pos = pos2;
        if (pos > 0)
          return path.substring(pos + 1);
        return path;
      }  public void zip(String fname) throws IOException {
        zip(new File(fname));
      }  public void unzip(InputStream in, OutputStream out) throws IOException {
        GZIPInputStream unzip = new GZIPInputStream(in);
        try {
          byte[] buffer = new byte[1024];
          int bytesRead;
          while ( (bytesRead = unzip.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
          }
        }
        finally {
          if (unzip != null) {
            try {
              unzip.close();
            }
            catch (Throwable e) {}
          }
        }
      }  public void unzip(File fin, File fout) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = new FileInputStream(fin);
          out = new FileOutputStream(fout);
          unzip(in, out);
          in.close();
          in = null;
          boolean deleted;
          try {
            deleted = fin.delete();
          }
          catch (Throwable e) {
            deleted = false;
          }
          if (!deleted)
            fin.deleteOnExit();
        }
        catch (IOException e) {
          if (out != null)
            try {
              out.close();
              out = null;
            }
            catch (Throwable ex) {}
          if (fout != null) {
            boolean deleted;
            try {
              deleted = fout.delete();
            }
            catch (Throwable ex) {
              deleted = false;
            }
            if (!deleted)
              fout.deleteOnExit();
          }
        }
        finally {
          if (in != null)
            try {
              in.close();
            }
            catch (Throwable e) {}
          if (out != null)
            try {
              out.close();
            }
            catch (Throwable e) {}
        }
      }  public void unzip(File fin, String filename) throws IOException {
        unzip(fin, new File(filename));
      }  public void unzip(String fin, String fout) throws IOException {
        unzip(new File(fin), new File(fout));
      }  public void unzip(String fin, File fout) throws IOException {
        unzip(new File(fin), fout);
      }  /**
       * 解压单个文件的压缩包,请注意如果压缩包有多个文件,不会成功也不会报错
       * @param f
       * @throws IOException
       */
      public void unzip(File f) throws IOException {
        String path = getDirname(f.getPath());
        String name = getBasename(f.getName());
        int len = name.length();
        File dir = new File(path);
        File fout = new File(dir, name.substring(0, len - 4));
        unzip(f, fout);
      }  /**
       * 解压单个文件的压缩包,请注意如果压缩包有多个文件,不会成功也不会报错
       * @param fname
       * @throws IOException
       */
      public void unzip(String fname) throws IOException {
        unzip(new File(fname));
      }  /**
       * 解压含有多个文件的ZIP包
       * @param zipFileName
       * @param extPlace
       */
      private static void extZipFileList(String zipFileName, String extPlace) {
        try {      ZipInputStream in = new ZipInputStream(new FileInputStream(
              zipFileName));      ZipEntry entry = null;      while ( (entry = in.getNextEntry()) != null) {        String entryName = entry.getName();        if (entry.isDirectory()) {
              File file = new File(extPlace + entryName);
              file.mkdirs();
              System.out.println("创建文件夹:" + entryName);
            }
            else {          FileOutputStream os = new FileOutputStream(extPlace
                  + entryName);// Transfer bytes from the ZIP file to the output file
              byte[] buf = new byte[1024];          int len;
              while ( (len = in.read(buf)) > 0) {
                os.write(buf, 0, len);
              }
              os.close();
              in.closeEntry();        }
          }    }
        catch (IOException e) {
          e.printStackTrace();
        }
        System.out.println("解压文件成功");
      }  public static void main(String[] args) throws IOException {
        Zip zip = new Zip();
        zip.zip("c:\\111.xls");
        zip.extZipFileList("c:\\22.zip","c:\\");
      }
    }
      

  3.   

    只是用stream读写的话不会改变文件的内容,但是如果用了jdom等解析xml后再写文件,则xml内所有的节点都会重新排序