使用Stored压缩方法,必须再 putNextEntry之前加上如下代码:
/** 如果是目录,则需要在写目录后面加上 */
zipEntry = new ZipEntry(entryName + "/");
/** ZipEntry.STORED */
zipEntry.setMethod(ZipEntry.STORED);
zipEntry.setCompressedSize(srcFile.length());
zipEntry.setSize(srcFile.length());
CRC32 crc = new CRC32();
crc.update(getFileBytes(srcFile));
zipEntry.setCrc(crc.getValue());
/** ZipEntry.STORED */
zos.putNextEntry(zipEntry);
/**
*
* 方法名:getFileBytes<br>
* 描述:获取文件的bytes<br>
* 创建时间:2016-12-14 下午2:23:33<br>
* @param file
* @return
* @throws FileNotFoundException
* @throws IOException<br>
*/
public static byte[] getFileBytes(File file) throws FileNotFoundException,
IOException {
byte[] buffer;
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
return buffer;
}