public static void zipfilepip() { long begintime = system.currenttimemillis(); try(writablebytechannel out = channels.newchannel(new fileoutputstream(zip_file))) { pipe pipe = pipe.open(); //异步任务 completablefuture.runasync(()->runtask(pipe)); //获取读通道 readablebytechannel readablebytechannel = pipe.source(); bytebuffer buffer = bytebuffer.allocate(((int) file_size)*10); while (readablebytechannel.read(buffer)>= 0) { buffer.flip(); out.write(buffer); buffer.clear(); } }catch (exception e){ e.printstacktrace(); } printinfo(begintime);}//异步任务public static void runtask(pipe pipe) { try(zipoutputstream zos = new zipoutputstream(channels.newoutputstream(pipe.sink())); writablebytechannel out = channels.newchannel(zos)) { system.out.println(begin); for (int i = 0; i < 10; i++) { zos.putnextentry(new zipentry(i+suffix_file)); filechannel jpgchannel = new fileinputstream(new file(jpg_file_path)).getchannel(); jpgchannel.transferto(0, file_size, out); jpgchannel.close(); } }catch (exception e){ e.printstacktrace(); }}
以上就是java如何压缩文件的详细内容。
