请问,如何用java语句 把多个文件打压成一个压缩包!
帮帮忙,最好有一段源代码,这样我能更好的理解

解决方案 »

  1.   


    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.Deflater;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;public class ZipCreateExample {   public static void main(String[] args) {
         System.out.println("Example of ZIP file creation.");     // Specify files to be zipped
         String[] filesToZip = new String[3];
         filesToZip[0] = "c:/1.txt";
         filesToZip[1] = "c:/2.txt";
         filesToZip[2] = "c:/3.txt";     byte[] buffer = new byte[18024];     // Specify zip file name
         String zipFileName = "c:/example.zip";     try {       ZipOutputStream out =
             new ZipOutputStream(new FileOutputStream(zipFileName));       // Set the compression ratio
           out.setLevel(Deflater.DEFAULT_COMPRESSION);       // iterate through the array of files, adding each to the zip file
           for (int i = 0; i < filesToZip.length; i++) {
             System.out.println(i);
             // Associate a file input stream for the current file
             FileInputStream in = new FileInputStream(filesToZip[i]);         // Add ZIP entry to output stream.
             out.putNextEntry(new ZipEntry(filesToZip[i]));         // Transfer bytes from the current file to the ZIP file
             //out.write(buffer, 0, in.read(buffer));         int len;
            while ((len = in.read(buffer)) > 0)
            {
            out.write(buffer, 0, len);
           }         // Close the current entry
             out.closeEntry();         // Close the current file input stream
             in.close();       }
           // Close the ZipOutPutStream
           out.close();
         }
         catch (IllegalArgumentException iae) {
           iae.printStackTrace();
         }
         catch (FileNotFoundException fnfe) {
           fnfe.printStackTrace();
         }
         catch (IOException ioe)
         {
         ioe.printStackTrace();
         }
       }
    }