压缩文件我用的jar:ant-1.7.1.jar
controller就不用展示了,简单的写一下主要的:
public static void main(String[] args) {
OutputStream output = new FileOutputStream("D:\\test.zip");//新建一个zip
String encode="UTF-8";//编码格式
morePicZip(urls, output, encode, compress);
}
/**
* 读取多个网络图片打成打成ZIP
* @param urls
* key = 图片名, value = 图片URL
* @param output
* @param encode 编码
* @param compress 是否压缩
*/
public static void morePicZip(Map<String, String> urls,
OutputStream output, String encode) {
ZipOutputStream zipStream = null;
try {
zipStream = getZipStreamEncode(output, encode);
Map<String, String> synUrls = Collections.synchronizedMap(urls);
Set<Entry<String, String>> set = synUrls.entrySet();
Iterator<Entry<String, String>> it = set.iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
compressZip(entry.getValue(), zipStream, entry.getKey());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (zipStream != null) {
zipStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 单个网络图片压缩
*
* @param url
* @param zipStream
* @param compress
*/
private static void onePicZip(String url, ZipOutputStream zipStream,
String fileName) throws Exception{
InputStream input = null;
try {
input = getInputStream(url);
zip(input, zipStream, fileName, compress);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(input != null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//设置编码
private static ZipOutputStream getZipStreamEncode(OutputStream output,
String encode) {
ZipOutputStream zipStream = new ZipOutputStream(output);
if (encode == null || "".equals(encode)) {
zipStream.setEncoding("GBK");
} else {
zipStream.setEncoding(encode);
}
return zipStream;
}
//通过url获取网络图片流
public static InputStream getInputStream(String path) {
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(path);
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置网络连接超时时间
httpURLConnection.setConnectTimeout(3000);
// 设置应用程序要从网络连接读取数据
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 从服务器返回一个输入流
inputStream = httpURLConnection.getInputStream();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputStream;
}
//压缩图片文件
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);
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();
}
}
}