今天有一个需求,是要把一个pdf文件压缩,再进行传输,而我已经对pdf进行了本地备份,所以对zip文件不作备份,直接转为字节流,进行操作
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
File fileEd = new File(clientPath + "\\bak\\" + pdfFileName);
fis = new FileInputStream(fileEd);
bos = new ByteArrayOutputStream(fis.available());
byte[] b = new byte[fis.available()];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
bos.flush();
byte[] buff = zip(fileName,b);
/**
* @param fileName zip文件名
* @param content 文件的字节流
*/
public static byte[] zip(String fileName,byte[] content) throws Exception {
byte[] b = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(bos);
ZipEntry entry = new ZipEntry(fileName);
entry.setSize(content.length);
zip.putNextEntry(entry);
zip.write(content);
zip.closeEntry();
zip.close();
b = bos.toByteArray();
bos.close();
return b;
}