preface
You need to put some data into a zip compressed package. You can use ZipOutputStream. ZipOutputStream can write content directly into a zip package. Generally, ZipOutputStream is created by encapsulating a FileOutputStream. Before writing a file, you need to call putNextEntry once, and then use write to write byte [] type data. When writing is complete, use colseEntry to end the packaging of the file. Of course, you can also write data directly into the compressed package through ZipOutputStream to build data in the compressed package.
use
public static void filetest() throws IOException {
String txtPath = "D:\\fileTest\\image\\2.txt"; String zipPath = "D:\\fileTest\\image\\2.zip"; //Compressed packet path String str = "test test123abc"; //Data to be written //Create a compressed package ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath)); //Encapsulate a file FileWriter fileWriter = null; try { fileWriter = new FileWriter(txtPath); fileWriter.write(str); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { log.error("fileWriter", e); } //Build a FileInputStream for the encapsulated files above FileInputStream fis = new FileInputStream(txtPath); //Create an empty file in the compressed package zipOutputStream.putNextEntry(new ZipEntry("Request.json")); //Write compressed file int len; byte[] buffer = new byte[1024]; //Byte array size adjustable //Read the fis byte stream and transfer it to the buffer byte array. After reading, fis is empty while ((len = fis.read(buffer)) > 0) { zipOutputStream.write(buffer, 0, len); } byte[] b = new byte[1024]; int a = fis.read(b); //Turn off package packaging zipOutputStream.closeEntry(); fis.close(); zipOutputStream.flush(); zipOutputStream.close(); }
Copy code
After running, the following files will be created:
A request will be generated in the compressed package JSON file, as shown in the figure:
Content and 2 Txt is the same as "test 123abc".
The above method is: first create 2 Txt, read 2 Txt, which is imported into a compressed package to form a file. With the same logic, we can read any other file and put them into a compressed package.
Import content directly into a compressed package
Of course, we can also directly import the data into the compressed package. The implementation is as follows:
public static void filetest() throws IOException {
String zipPath = "D:\\fileTest\\image\\3.zip"; //Compressed packet path String str1 = "test test123abc"; //Data to be written String str2 = "Test 2"; String Name1 = StringUtils.join("file.json"); //Files in compressed package String Name2 = StringUtils.join("file/File 1.json"); //Create files in the file directory in the compressed package //Create a compressed package ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath)); //Create files in a compressed package zipOutputStream.putNextEntry(new ZipEntry(Name1)); byte[] bytes1 = str1.getBytes(StandardCharsets.UTF_8); zipOutputStream.write(bytes1, 0, bytes1.length); //Write the data to the file in the compressed package zipOutputStream.closeEntry(); zipOutputStream.putNextEntry(new ZipEntry(Name2)); byte[] bytes2 = str2.getBytes(StandardCharsets.UTF_8); zipOutputStream.write(bytes2, 0, bytes2.length); zipOutputStream.closeEntry(); zipOutputStream.flush(); zipOutputStream.close();
}
Copy code
The above is to directly convert String type data into byte array and import it into compressed package to form two files:
File folder is file 1 json, the content inside is "test 2", file The content of json is "test 123abc".
last
If you think this article is a little helpful to you, give it a compliment. Or you can join my development exchange group: 1025263163 learn from each other, and we will have professional technical Q & A to solve doubts
If you think this article is useful to you, please click star: https://gitee.com/ZhongBangKeJi esteem it a favor!
PHP learning manual: https://doc.crmeb.com
Technical exchange forum: https://q.crmeb.com