一、主函数
public class ZipTest {
public static void main(String[] args) {
String dirPath = "D:\\Tools";
List<File> dirToFile = FileUtils.getDirToFile(dirPath);
System.out.println("文件数=" + dirToFile.size());
List<ZipContent> zipContentList = dirToFile.parallelStream().map(item -> {
try {
return CompletableFuture.supplyAsync(() -> {
String absolutePath = item.getAbsolutePath();
byte[] bytes = null;
try {
bytes = FileUtils.readFileToByte(item);
} catch (IOException e) {
e.printStackTrace();
}
String substring = absolutePath.substring(absolutePath.indexOf(dirPath) + dirPath.length()).replace("\\","/");
String substring1 = dirPath.substring(dirPath.indexOf("\\")+1);
ZipContent content = new ZipContent(bytes, substring1+substring);
System.out.println(content.getFilePath());
return content;
}).get(1, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
return null;
}).toList();
System.out.println("ZipList size=" + zipContentList.size());
byte[] compress = ZipUtils.compress(zipContentList);
System.out.println("压缩结果长度=" + compress.length);
File file = new File(dirPath + "/" + "outPutZip.zip");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
long bytes = FileUtils.writeBytesToFile(compress, file);
if (bytes > 0) {
System.out.println("压缩文件保存成功,length=" + bytes);
} else {
System.out.println("压缩文件保存失败");
}
}
}
二、非递归遍历文件夹结构
public static List<File> getDirToFile(String path) {
CopyOnWriteArrayList<File> arrayList = new CopyOnWriteArrayList<>();
int fileNum = 0, folderNum = 0;
File file = new File(path);
LinkedList<File> list = new LinkedList<File>();
if (file.exists()) {
File[] files = file.listFiles();
if (files != null) {
for (File file2 : files) {
if (file2.isDirectory()) {
list.add(file2);
} else {
fileNum++;
arrayList.add(file2);
}
}
}
File temp_file;
while (!list.isEmpty()) {
temp_file = list.removeFirst();
files = temp_file.listFiles();
if (files != null) {
for (File file2 : files) {
if (file2.isDirectory()) {
list.add(file2);
folderNum++;
} else {
fileNum++;
arrayList.add(file2);
}
}
}
}
}
return arrayList;
}
三、二进制保存为文件
public static long writeBytesToFile(byte[] bytes, File file) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
byteArrayInputStream.transferTo(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return file.length();
}
四、读取文件为二进制
public static byte[] readFileToByte(File file) throws IOException {
BufferedInputStream bf = new BufferedInputStream(new FileInputStream(file));
try {
byte[] data = new byte[bf.available()];
bf.read(data);
return data;
} finally {
bf.close();
}
}
五、内存压缩工具包
/**
* 内存压缩工具包
*
* @param dataList
* @return
*/
public static byte[] compress(List<ZipContent> dataList) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
ByteArrayOutputStream zipBos = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(zipBos);
try {
for (ZipContent zipContent : dataList) {
String filePath = zipContent.getFilePath();
byte[] bytes = zipContent.getBytes();
ZipEntry zipEntry = new ZipEntry(filePath);
zipOutputStream.putNextEntry(zipEntry);
WritableByteChannel writableByteChannel = Channels.newChannel(zipOutputStream);
writableByteChannel.write(ByteBuffer.wrap(bytes));
}
zipOutputStream.closeEntry();
zipOutputStream.finish();
} catch (Exception e) {
e.printStackTrace();
}
return zipBos.toByteArray();
}
运行结果:

版权声明:本文为qq_41617848原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。