During the work process, a folder needs to be generated to be compressed and then provided to the user for download. So I wrote a tool class for compressed files myself. This tool class supports single file and folder compression. Put the 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 tool class* Compress the specified file/folder into zip and rar compressed files*/public class CompressedFileUtil { /** * Default constructor */ public CompressedFileUtil(){ } /** * @desc Generate the source file/folder in the specified format to compressed files, format zip * @param resourcePath Source file/folder * @param targetPath Destination compressed file saving path * @return void * @throws Exception */ public void compressedFile(String resourcesPath,String targetPath) throws Exception{ File resourcesFile = new File(resourcesPath); //Source file File targetFile = new File(targetPath); //Purpose//If the destination path does not exist, create a new if(!targetFile.exists()){ targetFile.mkdirs(); } String targetName = resourcesFile.getName()+".zip"; //Target compressed file name FileOutputStream outputStream = new FileOutputStream(targetPath+"//"+targetName); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream)); createCompressedFile(out, resourcesFile, ""); out.close(); } /** * @desc Generate compressed file. * If it is a folder, then use recursion to traverse and compress the file* If it is a file, directly compress * @param out Output Stream* @param file Target file* @return void * @throws Exception */ public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{ //If the current is a folder, further processing is performed if(file.isDirectory()){ //Get file list information File[] files = file.listFiles(); //Add folder to the next level packaging directory out.putNextEntry(new ZipEntry(dir+"/")); dir = dir.length() == 0 ? "" : dir +"/"; //Loop to package files in the folder for(int i = 0 ; i < files.length ; i++){ createCompressedFile(out, files[i], dir + files[i].getName()); //Recursive processing} } else{ //The current is a file, packaging processing//File input stream FileInputStream fis = new FileInputStream(file); out.putNextEntry(new ZipEntry(dir)); //Write 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("Compressed file has been generated..."); } catch (Exception e) { System.out.println("Compressed file generation failed..."); e.printStackTrace(); } }}The results of running the program are as follows:
The file directory structure before compression:
Tip: If you use java.util.zip under java.util for packaging, there may be a problem of garbled Chinese. This is because the java zip method does not support changes in encoding format. We can use the zip tool class under ant.java for packaging. Therefore, you need to import ant.jar into the lib directory of the project.
Summarize
The above is the example code for Java generated compressed files introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!