如何用java解压一个zip或rar的文件
比如C:\Program Files\a.zip解开后是C:\Program Files\a
不能调winrar要用程序,最好写全点,多谢

解决方案 »

  1.   

    //: c12:ZipCompress.java
    //Uses Zip compression to compress any
    //number of files given on the command line.
    //{Args: ZipCompress.java}
    //{Clean: test.zip}
    import com.bruceeckel.simpletest.*;
    import java.io.*;
    import java.util.*;
    import java.util.zip.*;public class ZipCompress {
    private static Test monitor = new Test(); // Throw exceptions to console:
    public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");
    // No corresponding getComment(), though.
    for (int i = 0; i < args.length; i++) {
    System.out.println("Writing file " + args[i]);
    BufferedReader in = new BufferedReader(new FileReader(args[i]));
    zos.putNextEntry(new ZipEntry(args[i]));
    int c;
    while ((c = in.read()) != -1)
    out.write(c);
    in.close();
    }
    out.close();
    // Checksum valid only after the file has been closed!
    System.out.println("Checksum: " + csum.getChecksum().getValue());
    // Now extract the files:
    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
    System.out.println("Reading file " + ze);
    int x;
    while ((x = bis.read()) != -1)
    System.out.write(x);
    }
    if (args.length == 1)
    monitor.expect(new String[] { "Writing file " + args[0],
    "%% Checksum: \\d+", "Reading file",
    "Reading file " + args[0] }, args[0]);
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();
    // Alternative way to open and read zip files:
    ZipFile zf = new ZipFile("test.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
    ZipEntry ze2 = (ZipEntry) e.nextElement();
    System.out.println("File: " + ze2);
    // ... and extract the data as before
    }
    if (args.length == 1)
    monitor.expect(new String[] { "%% Checksum: \\d+",
    "File: " + args[0] });
    }
    } // /:~
      

  2.   

    try {
            // Open the ZIP file
            String inFilename = "infile.zip";
            ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));
        
            // Get the first entry
            ZipEntry entry = in.getNextEntry();
        
            // Open the output file
            String outFilename = "o";
            OutputStream out = new FileOutputStream(outFilename);
        
            // Transfer bytes from the ZIP file to the output file
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        
            // Close the streams
            out.close();
            in.close();
        } catch (IOException e) {
        }
      

  3.   

    我在说一遍楼上的你试过这个程序吗根本出不来看好要求呀,要求是像winrar一样把那个zip文件解压成一个文件夹就成了。希望楼下的能给个好答案,急
      

  4.   


    /**
     * @param args
     */
    public static void main(String[] args) {
    extZipFileList("D:/temp/zipFile.zip", "D:/temp/zipFile1/"); } public 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 { OutputStream 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();
    System.out.println("解压文件:" + entryName);
    }
    }
    } catch (IOException e) {
    }
    }
    很简单的,自己多动下手吧.
    建议有时间多参考一下:
    http://javaalmanac.com
      

  5.   

    还是不成呀,我把题目在说一遍,就是C:\Program Files\a.zip解完后C:\Program Files下出现一个叫a的文件夹。里面的文件内容不用管。只要像rar那样解开就可以,希望能给我可以能运行的程序,上面的没一个能运行出结果的,多谢各位了,只要解决了我把号里的分都给你,觉少不了500
      

  6.   

    楼主信誉值这么低,真是浪费了[eureka0891(迷茫中...)兄]的表情.
      

  7.   

    import java.io.*;import java.util.zip.*;public class ZipFileList {
    public static void main(String[] args) {
    extZipFileList("E:/software/lumaqq_2004-win32_x86_with_jre.zip", "E:/software/"); } 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) {
    }
    System.out.println("解压文件成功");
    }
    }