工具類一(将多個遠端檔案讀取并壓縮)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
public class ZipUtil {
/**
* 壓縮檔案
* @param files 鍵值對-》(檔案名:檔案連結)
* @param outputStream
* @throws Exception
* @throws IOException
*/
public static void zipPersonPhotoFile(Map<String,String> files, ZipOutputStream outputStream) {
try {
Set<Entry<String, String>> entrySet = files.entrySet();
for (Entry<String, String> file:entrySet) {
try {
zipFile(getImgIs(file.getValue()),file.getKey(), outputStream);
} catch (Exception e) {
continue;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 将檔案寫入到zip檔案中
* @param inputFile
* @param outputstream
* @throws Exception
*/
public static void zipFile(InputStream is, String fileName, ZipOutputStream outputstream) throws IOException, ServletException {
try {
if (is != null) {
BufferedInputStream bInStream = new BufferedInputStream(is);
ZipEntry entry = new ZipEntry(fileName);
outputstream.putNextEntry(entry);
int len = 0 ;
byte[] buffer = new byte[10 * 1024];
while ((len = is.read(buffer)) > 0) {
outputstream.write(buffer, 0, len);
outputstream.flush();
}
outputstream.closeEntry();//Closes the current ZIP entry and positions the stream for writing the next entry
bInStream.close();//關閉
is.close();
} else {
throw new ServletException("檔案不存在!");
}
} catch (IOException e) {
throw e;
}
}
/**
* 擷取檔案流
*/
public static InputStream getImgIs(String imgURL) throws IOException{
//new一個URL對象
URL url = new URL(imgURL);
//打開連結
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設定請求方式為"GET"
conn.setRequestMethod("GET");
//逾時響應時間為5秒
conn.setConnectTimeout(5 * 1000);
//通過輸入流擷取圖檔資料
return conn.getInputStream();
}
/**
* 下載下傳打包的檔案
*
* @param file
* @param response
*/
public static void downloadZip(File file, HttpServletResponse response) {
try {
// 以流的形式下載下傳檔案。
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
toClient.write(buffer);
toClient.flush();
toClient.close();
file.delete();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
執行個體
public void zipFile (Map<String,String> map) {
try {
ZipUtil.zipPersonPhotoFile(map, new ZipOutputStream(new FileOutputStream(new File(zipFilePath))));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
工具類二(将本地某個檔案/檔案夾壓縮)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import org.apache.commons.io.FileUtils;
public class ZipUtils {
private ZipUtils() {
throw new IllegalStateException("Utility class");
}
/**
* 将檔案/目錄進行壓縮
* @param sourceFile 原檔案/目錄
* @param targetZipFile 壓縮後目标檔案
* @throws IOException
*/
public static void zipFiles(File sourceFile, File targetZipFile) throws IOException {
ZipOutputStream outputStream = null;
try {
outputStream = new ZipOutputStream(new FileOutputStream(targetZipFile));
addEntry("", sourceFile, outputStream);
} catch (Exception e) {
throw new IOException(e);
} finally {
outputStream.close();
}
}
/**
* 将檔案寫入到zip檔案中
* @param source
* @param outputstream
* @throws IOException
* @throws ServletException
*/
private static void addEntry(String base, File source, ZipOutputStream outputstream)
throws IOException, ServletException {
FileInputStream is = null;
try {
String entry = base + source.getName();
if (source.isDirectory()) {
for (File file : source.listFiles()) {
// 遞歸導入檔案
addEntry(entry + File.separator, file, outputstream);
}
} else {
is = FileUtils.openInputStream(source);
if (is != null) {
outputstream.putNextEntry(new ZipEntry(entry));
int len = 0;
byte[] buffer = new byte[10 * 1024];
while ((len = is.read(buffer)) > 0) {
outputstream.write(buffer, 0, len);
outputstream.flush();
}
outputstream.closeEntry();
}
}
} catch (IOException e) {
throw e;
} finally {
if (is != null) {
is.close();
}
}
}
}