This example shares the specific code exported by multiple Java files in ZIP compression packages for your reference. The specific content is as follows
1. Use Java to implement the image of the bar server into a compressed package in zip format and export multiple files in package.
2. The code is as follows:
**ImageByteUtil.java**
public class ImageByteUtil{ private static float QUALITY = 0.6f; public static void compressZip(List<File> listfiles, OutputStream output,String encode, boolean compress,String alias){ ZipOutputStream zipStream = null; try { zipStream = new ZipOutputStream(output); for (File file : listfiles){ compressZip(file, zipStream, compress,alias+"_"+(listfiles.indexOf(file)+1)); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (zipStream != null) { zipStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } private static void compressZip(File file, ZipOutputStream zipStream, boolean compress,String alias) throws Exception{ FileInputStream input = null; try { input = new FileInputStream(file); //zip(input, zipStream, file.getName(), compress); zip(input, zipStream, alias+"."+file.getName().substring(file.getName().lastIndexOf(".")+1), compress); } catch (Exception e) { e.printStackTrace(); } finally { try { if(input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } } }private static void zip(InputStream input, ZipOutputStream zipStream, String zipEntryName, boolean compress) throws Exception{ byte[] bytes = null; BufferedInputStream bufferStream = null; try { if(input == null) throw new Exception("Failed to obtain compressed data items! The data item name is: " + zipEntryName); // The compressed entry is not a specific independent file, but a list item in the list of the compressed package file, called an entry, just like an index ZipEntry zipEntry = new ZipEntry("Image/"+zipEntryName); // Position to the compressed entry position and start writing the file to the compressed package zipStream.putNextEntry(zipEntry); if (compress) { bytes = ImageByteUtil.compressOfQuality(input, 0); zipStream.write(bytes, 0, bytes.length); } else { bytes = new byte[1024 * 5];// Read and write buffer bufferStream = new BufferedInputStream(input);// Input buffer int read = 0; while ((read = bufferStream.read(bytes)) != -1) { zipStream.write(bytes, 0, read); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != bufferStream) bufferStream.close(); } catch (IOException e) { e.printStackTrace(); } } } public static byte[] compressOfQuality(File file, float quality) throws Exception{ byte[] bs = null; InputStream input = null; try { input = new FileInputStream(file); bs = compressOfQuality(input,quality); } catch (Exception e) { e.printStackTrace(); } finally { try { if (input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } } return bs; } public static byte[] compressOfQuality(InputStream input, float quality) throws Exception { ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); if(quality == 0){ Thumbnails.of(input).scale(1f).outputQuality(QUALITY) .toOutputStream(output); } else { Thumbnails.of(input).scale(1f).outputQuality(quality).toOutputStream(output); } return output.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (output != null) output.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }}**Main.java**
public static void main(String[] args){ //Add the file collection to be exported, add the file you need to export List<File> ListFiles = new ArrayList<>(); //Calling the tool class, parameter description (the file set that needs to be exported, ByteArrayOutputStream object, encoding, whether to compress [true, false], file name prefix) ImageByteUtil.compressZip(ListFiles, out, "UTF-8", false,"LWJ"); //Specify the export format ReturnContext.addParam("exportFileName","extFile.zip"); ReturnContext.addParam("mimeType", "zip"); return in;}3. This function is implemented according to the project needs during the development process. The test can be used normally and can be customized.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.