Java generates compressed file sample code

  • 2020-04-01 02:33:49
  • OfStack

Code:


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/** 
 * @project: Test 
 * @author chenssy
 * @date 2013-7-28 
 * @Description:  File compression utility class 
 *                    Will specify the file / Folder compressed into zip , rar The compressed file 
 */
public class CompressedFileUtil {
    
    public CompressedFileUtil(){

    }
    /**
     * @desc  will The source file/ Folder generates a compressed file in the specified format , format zip
     * @param resourePath The source file/ folder 
     * @param targetPath  purpose Zip file save path 
     * @return void
     * @throws Exception 
     */
    public void compressedFile(String resourcesPath,String targetPath) throws Exception{
        File resourcesFile = new File(resourcesPath);     //The source file
        File targetFile = new File(targetPath);           //purpose
        // if purpose Path does not exist, new 
        if(!targetFile.exists()){     
            targetFile.mkdirs();  
        }

        String targetName = resourcesFile.getName()+".zip";   //purpose Compressed file name 
        FileOutputStream outputStream = new FileOutputStream(targetPath+"\"+targetName);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));

        createCompressedFile(out, resourcesFile, "");

        out.close();  
    }

    
    public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
        //If the current one is a folder, further processing occurs
        if(file.isDirectory()){
            //Gets the file list information
            File[] files = file.listFiles();
            //Add the folder to the next level of the packaging directory
            out.putNextEntry(new ZipEntry(dir+"/"));

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

            //Loop packages files in folders
            for(int i = 0 ; i < files.length ; i++){
                createCompressedFile(out, files[i], dir + files[i].getName());         //Recursive processing
            }
        }
        else{   //The current one is a file, packaged
            //File input stream
            FileInputStream fis = new FileInputStream(file);

            out.putNextEntry(new ZipEntry(dir));
            //Write operation
            int j =  0;
            byte[] buffer = new byte[1024];
            while((j = fis.read(buffer)) > 0){
                out.write(buffer,0,j);
            }
            //Close the input stream
            fis.close();
        }
    }

    public static void main(String[] args){
        CompressedFileUtil compressedFileUtil = new CompressedFileUtil();

        try {
            compressedFileUtil.compressedFile("G:\zip", "F:\zip");
            System.out.println(" The compressed file has been generated ...");
        } catch (Exception e) {
            System.out.println(" Compressed file generation failed ...");
            e.printStackTrace();
        }
    }
}


Related articles: