压缩多个oss文件
private File zipFileListFromObj(List<OSSObject> filenames ,List<String> show_files) throws IOException {
String filename = "临时名称"+".zip";
byte[] buffer = new byte[8192];
String ziptemp = ziptempPath;
String strZipPath = ziptemp+filename;
File path = new File(ziptemp);
if(!path.exists()) {
path.mkdir();
}
File f = new File(strZipPath);
ZipOutputStream out =null;
try {
out = new ZipOutputStream(new FileOutputStream(strZipPath)) ;
out.setEncoding("gbk");
for (int i = 0; i < filenames.size(); i++) {
OSSObject ossObject = filenames.get(i);
BufferedInputStream bufferedInputStream = new BufferedInputStream(ossObject.getObjectContent());
out.putNextEntry(new ZipEntry((String)show_files.get(i)));
int len;
while ((len = bufferedInputStream.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
bufferedInputStream.close();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
log.error("文件不存在,打包失败!");
throw new IOException("文件不存在,打包失败!");
}
finally
{
if(out!=null)
{
out.flush();
out.close();
}
}
return f;
}
压缩多个文件 File
private File zipFileList(List<File> filenames ,List<String> show_files) throws IOException {
String filename = buildTempName()+".zip";
byte[] buffer = new byte[8192];
String ziptemp = ziptempPath;
String strZipPath = ziptemp+filename;
File path = new File(ziptemp);
if(!path.exists()) {
path.mkdir();
}
File f = new File(strZipPath);
ZipOutputStream out =null;
try {
out = new ZipOutputStream(new FileOutputStream(strZipPath)) ;
out.setEncoding("gbk");
for (int i = 0; i < filenames.size(); i++) {
File file = filenames.get(i);
if(!file.exists()) {
log.error("文件"+file.getName()+"不存在,打包失败!");
throw new IOException("文件"+file.getName()+"不存在,打包失败!");
}
FileInputStream fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry((String)show_files.get(i)));
int len;
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
log.error("文件不存在,打包失败!");
throw new IOException("文件不存在,打包失败!");
}
finally
{
if(out!=null)
{
out.flush();
out.close();
}
}
return f;
}
解压zip
public static byte[] unGZip(byte[] data) {
byte[] b = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
gzip.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}