java代码如何将拆分后的zip文件合并一个zip文件呢?
今天接到一个需求,需将一个200MB的zip文件合并
当初压缩时,分了8个小的zip文件,那么如何实现这个zip文件合并的需求呢?下文笔者将一一道来,如下所示
当初压缩时,分了8个小的zip文件,那么如何实现这个zip文件合并的需求呢?下文笔者将一一道来,如下所示
合并ZIP的实现思路
1.定义ZipFile对象 2.生成ZipEntry对象 向ZipFile对象中添加文件 采用这种方式,即可实现Zip文件的合并操作
合并ZIP的示例
public static void splitZip(String zipName, String location, String NewZip) throws IOException{ FileInputStream fis = new FileInputStream(location); ZipInputStream zipInputStream = new ZipInputStream(fis); ZipEntry entry = null; int currentChunkIndex = 0; long entrySize = 0; ZipFile zipFile = new ZipFile(location); Enumeration enumeration = zipFile.entries(); String copDest = zipCopyDest + "\\" + NewZip + "_" + currentChunkIndex +".zip"; FileOutputStream fos = new FileOutputStream(new File(copDest)); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos); long currentSize = 0; try { while ((entry = zipInputStream.getNextEntry()) != null && enumeration.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enumeration.nextElement(); System.out.println(zipEntry.getName()); System.out.println(zipEntry.getSize()); entrySize = zipEntry.getSize(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); if((currentSize + entrySize) > MAX_FILE_SIZE) { zos.close(); currentChunkIndex++; zos = getOutputStream(currentChunkIndex, NewZip); currentSize = 0; }else{ currentSize += entrySize; zos.putNextEntry(new ZipEntry(entry.getName())); byte[] buffer = new byte[8192]; int length = 0; while ((length = zipInputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } byte[] unzippedFile = outputStream.toByteArray(); zos.write(unzippedFile); unzippedFile = null; outputStream.close(); zos.closeEntry(); } } } finally { zos.close(); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。