JAVA實現多檔案以ZIP壓縮包匯出
1、使用java實現吧伺服器的圖片打包成一個zip格式的壓縮包匯出,多個檔案打包匯出。
2、程式碼如下:
**ImageByteUtil.java**
public class ImageByteUtil{
private static float QUALITY = 0.6f;
public static void compressZip(List<File> listfiles, OutputStream output,String encode, boolean compress,String alias){
ZipOutputStream zipStream = null;
try {
zipStream = new ZipOutputStream(output);
for (File file : listfiles){
compressZip(file, zipStream, compress,alias+"_"+(listfiles.indexOf(file)+1));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (zipStream != null) {
zipStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void compressZip(File file, ZipOutputStream zipStream,
boolean compress,String alias) throws Exception{
FileInputStream input = null;
try {
input = new FileInputStream(file);
//zip(input, zipStream, file.getName(), compress);
zip(input, zipStream, alias+"."+file.getName().substring(file.getName().lastIndexOf(".")+1), compress);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(input != null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void zip(InputStream input, ZipOutputStream zipStream,
String zipEntryName, boolean compress) throws Exception{
byte[] bytes = null;
BufferedInputStream bufferStream = null;
try {
if(input == null)
throw new Exception("獲取壓縮的資料項失敗! 資料項名為:" + zipEntryName);
// 壓縮條目不是具體獨立的檔案,而是壓縮包檔案列表中的列表項,稱為條目,就像索引一樣
ZipEntry zipEntry = new ZipEntry("圖片/"+zipEntryName);
// 定位到該壓縮條目位置,開始寫入檔案到壓縮包中
zipStream.putNextEntry(zipEntry);
if (compress) {
bytes = ImageByteUtil.compressOfQuality(input, 0);
zipStream.write(bytes, 0, bytes.length);
} else {
bytes = new byte[1024 * 5];// 讀寫緩衝區
bufferStream = new BufferedInputStream(input);// 輸入緩衝流
int read = 0;
while ((read = bufferStream.read(bytes)) != -1) {
zipStream.write(bytes, 0, read);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != bufferStream)
bufferStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static byte[] compressOfQuality(File file, float quality) throws Exception{
byte[] bs = null;
InputStream input = null;
try {
input = new FileInputStream(file);
bs = compressOfQuality(input,quality);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bs;
}
public static byte[] compressOfQuality(InputStream input, float quality)
throws Exception {
ByteArrayOutputStream output = null;
try {
output = new ByteArrayOutputStream();
if(quality == 0){
Thumbnails.of(input).scale(1f).outputQuality(QUALITY)
.toOutputStream(output);
} else {
Thumbnails.of(input).scale(1f).outputQuality(quality).toOutputStream(output);
}
return output.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
**Main.java**
public static void main(String[] args){
//要匯出的檔案集合,新增自己需要匯出的檔案
List<File> ListFiles = new ArrayList<>();
//呼叫工具類,引數說明(需要匯出的檔案集,ByteArrayOutputStream物件,編碼,是否壓縮【true,false】,檔名稱字首)
ImageByteUtil.compressZip(ListFiles, out, "UTF-8", false,"LWJ");
//指定匯出格式
ReturnContext.addParam("exportFileName","extFile.zip");
ReturnContext.addParam("mimeType", "zip");
return in;
}
3、此功能是根據在開發過程中專案需要實現的,測試可正常使用,可更改定製。
---------------------
原文:https://blog.csdn.net/lovelongjun/article/details/76104192