public static void filetest() throws IOException {
String zipPath = "D:\\fileTest\\image\\3.zip"; //压缩包路径
String str1 = "测试test123abc"; //需要写入的数据
String str2 = "测试2";
String Name1 = StringUtils.join("文件.json"); //压缩包里的文件
String Name2 = StringUtils.join("file/文件1.json"); //在压缩包里创建file目录下的文件
//创建压缩包
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
//与方式二匹配使用(使用缓冲流)
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOutputStream);
//创建压缩包里的文件
zipOutputStream.putNextEntry(new ZipEntry(Name1));
byte[] bytes1 = str1.getBytes(StandardCharsets.UTF_8);
zipOutputStream.write(bytes1, 0, bytes1.length); //将数据写入到压缩包里的文件里面
zipOutputStream.closeEntry();
zipOutputStream.putNextEntry(new ZipEntry(Name2));
byte[] bytes2 = str2.getBytes(StandardCharsets.UTF_8);
//方式一将数据写入文件(不适用缓冲流)
// zipOutputStream.write(bytes2, 0, bytes2.length);
//方式二将数据写入文件(使用缓存流)
bufferedOutputStream.write(bytes, 0, bytes.length);
zipOutputStream.closeEntry();
zipOutputStream.flush();
zipOutputStream.close();
}
- f ile文件夹里面是文件1.json,里面的内容是“测试2”,文件.json的内容则是“测试test123abc”。
使用缓冲流将文件写入json文件
public static void writeString(File file, String content) {
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(content);
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭流释放资源
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//测试
public static void main(String[] args) {
long stime1 = System.currentTimeMillis();
for (int j = 0; j <100 ; j++) {
File file = new File("G://wyh//out"+j+".json");
boolean b = file.exists();
if (!b) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
List<Map<String, Object>> list = new ArrayList<>();
int[] ints = new int[]{1,2,34,2,3,2};
Map<String, Object> map = new HashMap<>();
Map<String, Object> map1 = new HashMap<>();
list.add(map);
for (int i = 0; i < 100000; i++) {
map.put("Image"+i,"图片");
map.put("Width"+i,"800");
map.put("Height"+i,"Height");
map.put("Title"+i,"View from 15th Floor");
map1.put("Image","图片");
map1.put("Width","800");
map1.put("Height","Height");
map1.put("Title","View from 15th Floor");
map.put("IDsmap"+i,map1);
map.put("IDs"+i,ints);
}
String json = JSON.toJSONString(map);
writeString(file, json);
}
long etime1 = System.currentTimeMillis();
System.out.println("-----------end-----------"+(etime1 - stime1));
}
多文件压缩
package com.edc.erp.task.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 压缩多个文件
*/
public class ZipMultiFile {
public static void main(String[] args) {
File[] srcFiles = {new File(""), new File("D:\\hhh\\Massage-app.zip")};
File zipFile = new File("D:\\hhh\\"+System.currentTimeMillis()+".zip");
// 调用压缩方法
zipFiles(srcFiles, zipFile);
System.out.println("压缩完成!");
}
public static void zipFiles(File[] srcFiles, File zipFile) {
// 判断压缩后的文件存在不,不存在则创建
if (!zipFile.exists()) {
try {
zipFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建 FileOutputStream 对象
FileOutputStream fileOutputStream = null;
// 创建 ZipOutputStream
ZipOutputStream zipOutputStream = null;
// 创建 FileInputStream 对象
FileInputStream fileInputStream = null;
try {
// 实例化 FileOutputStream 对象
fileOutputStream = new FileOutputStream(zipFile);
// 实例化 ZipOutputStream 对象
zipOutputStream = new ZipOutputStream(fileOutputStream);
// 创建 ZipEntry 对象
ZipEntry zipEntry = null;
// 遍历源文件数组
for (int i = 0; i < srcFiles.length; i++) {
// 将源文件数组中的当前文件读入 FileInputStream 流中
fileInputStream = new FileInputStream(srcFiles[i]);
// 实例化 ZipEntry 对象,源文件数组中的当前文件
zipEntry = new ZipEntry(srcFiles[i].getName());
zipOutputStream.putNextEntry(zipEntry);
// 该变量记录每次真正读的字节个数
int len;
// 定义每次读取的字节数组
byte[] buffer = new byte[1024];
while ((len = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
}
zipOutputStream.closeEntry();
zipOutputStream.close();
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}