最近想写个东西,要使用分卷压缩,但是不想使用别的开源包,自己写了一个类来实现,其实使用
public void zip(String path) throws IOException {
File file = new File(path);BufferedInputStream origin = null;
byte data[] = new byte[BUFFER];FileInputStream fi = new FileInputStream(file);origin = new BufferedInputStream(fi, BUFFER);int count;int offset = 0;ZipOutputStream out = null;while (true) { if ((count = origin.read(data, 0, BUFFER)) != -1) { if (offset % (MEGA) == 0) { FileOutputStream dest = new FileOutputStream(file.getName()+ ".(part" + (offset / (MEGA) + 1) + ").zip");out = new ZipOutputStream(new BufferedOutputStream(dest));ZipEntry entry = new ZipEntry(file.getName());out.putNextEntry(entry);}out.write(data, 0, count);offset += 1024;if (offset % (MEGA) == 0 && offset != 0) { out.close();}} else { out.close();break;}}origin.close();}public void unzip(String[] path) throws IOException {
File file = new File(path[0].split("\\.")[0] + "."+ path[0].split("\\.")[1]);BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file));byte data[] = new byte[BUFFER];int offset = 0;for (int i = 0; i < path.length; i++) { ZipInputStream unzipfile = new ZipInputStream(new FileInputStream(path[0]));BufferedInputStream in = new BufferedInputStream(unzipfile);ZipEntry entry = unzipfile.getNextEntry();int count = 0;while ((count = in.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count);}in.close();}out.close();}这里不要忘记了,压缩和解压都要使用entry来获得该文件的入口ZipEntry提供了一个功能很广泛的接口,允许我们获取设置zip文件内该特定项上的可利用的数据:名字压缩和未压缩的文件大小,日期校验和等