Java generate compressed files (zip, rar format

package com.demo.student.util;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io. *;

/ **
* generate a compressed file (zip, rar format)
* /
public class CompressUtil {  

  / **
  * @param path to compress the file path
  * @param format generated by the format (ZIP, RAR) D
  * /
  public static void generateFile (String path, the format String) throws Exception {

  File File = new new File (path);
  // compressed file path does not exist
  (! File.Exists ()) {IF
  the throw new new Exception ( "path" + path + "file does not exist, can not be compressed ...") ;
  }
  // used to store the compressed file folder
  String generateFile = file.getParent () + + File.separator "CompressFile";
  file the compress = new new file (generateFile);
  // If the folder does not exist, create it
  if ( ! compress.exists ()) {
  compress.mkdirs ();
  }

  // The purpose compressed files
  String generateFileName = compress.getAbsolutePath () + File.separator + "AAA" + file.getName () + + format ".";

  // read the data from the input stream represents a source
  // stream represents the output data is written to a certain

  // 输出流
  FileOutputStream outputStream = new FileOutputStream(generateFileName);

  // compressed output stream
  ZipOutputStream zipOutputStream = new ZipOutputStream (new BufferedOutputStream (outputStream));

  generateFile(zipOutputStream,file,"");

  System.out.println ( "source file location:" + file.getAbsolutePath () + " , the compressed file generating object positions:" + generateFileName);
  // close the output stream
  zipOutputStream.close ();
}

/ **
* @param OUT output stream
* @param file the target file
* @param dir folder
* @throws Exception
* /
Private static void generateFile (ZipOutputStream OUT, File File, String dir) throws Exception {

  // The current folder is then further processed for
  IF (file.isDirectory ()) {
    // get the file list information
    File [] files = file.listFiles () ;

    // add a folder to the next level package directory
    out.putNextEntry (new ZipEntry (dir + " /"));

    dir = dir.length() == 0 ? "" : dir + "/";

    // Loop file folder package
    for (int I = 0; I <files.length; I ++) {
    generateFile (OUT, Files [I], the dir + Files [I] .getName ());
  }

  } Else {// file is currently

    // input stream
    the FileInputStream = new new inputStream the FileInputStream (File);
    // tag entries to be packaged
    out.putNextEntry (the ZipEntry new new (the dir));
    // write
    int len = 0;
    byte [] bytes = new new byte [1024 ];
    the while ((len = InputStream.read (bytes))> 0) {
    out.write (bytes, 0, len);
  }
  // close the input stream
  inputStream.close ();
  }

}

  // 测试
public static void main(String[] args) {
  String path = "";
  String format = "rar";

  try {
     generateFile(path, format);
   } catch (Exception e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
    }

  }

}

Guess you like

Origin www.cnblogs.com/thcy1314/p/11280521.html