import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final int BUFFER_SIZE = 2 * 1024;
public static void toZip(List<File> sourceFiles, OutputStream fos) {
ZipOutputStream zos = null;
FileInputStream fis = null;
try {
zos = new ZipOutputStream(fos);
if (!sourceFiles.isEmpty()) {
for (File file : sourceFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(file.getName()));
fis = new FileInputStream(file);
int len;
while ((len = fis.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
}
}
} catch (Exception e) {
throw new RuntimeException("Compressed file error!!!", e);
} finally {
try {
if (fis != null) {
fis.close();
}
if (zos != null) {
zos.close();
}
if (fos != null) {
fos.flush();
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static final String FILE_URL = "FILE_URL";
public static final String FILE_NAME = "FILE_NAME";
public static final String FILE_DIRECTORY = "FILE_DIRECTORY";
public static final String SLASH = "/";
public static Map<String, Object> getToZipUrlMap(String fileUrl, String fileName,String fileDirectory){
Map<String, Object> map = new HashMap<String, Object>();
if (fileUrl != null) {
StringBuffer buffer=new StringBuffer();
String[] urls = fileUrl.split("/");
String[] urlss = urls[urls.length - 1].split("\\.");
map.put(FILE_URL, fileUrl);
buffer.append((fileName == null ? urlss[urlss.length - 2] : fileName));
buffer.append("." + urlss[urlss.length - 1]);
map.put(FILE_NAME, buffer.toString());
if(fileDirectory != null) {
map.put(FILE_DIRECTORY, fileDirectory+SLASH);
}
}
return map;
}
public static void toZipHTTP(List<Map<String, Object>> fileHttps, OutputStream os) {
ZipOutputStream zos = null;
URL url = null;
URLConnection con = null;
InputStream fis = null;
try {
if (!fileHttps.isEmpty()) {
zos = new ZipOutputStream(os);
for (Map<String, Object> map : fileHttps) {
if (map.containsKey(FILE_URL) && map.containsKey(FILE_NAME)) {
byte[] buf = new byte[BUFFER_SIZE];
String urls = map.get(FILE_URL).toString();
if (map.containsKey(FILE_DIRECTORY)) {
zos.putNextEntry(new ZipEntry(map.get(FILE_DIRECTORY).toString() + map.get(FILE_NAME).toString()));
} else {
zos.putNextEntry(new ZipEntry(map.get(FILE_NAME).toString()));
}
url = new URL(urls);
con = url.openConnection();
fis = con.getInputStream();
int len;
while ((len = fis.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
System.out.println("Packaged successfully!");
fis.close();
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
List<Map<String, Object>> sourceMapList = new ArrayList<Map<String, Object>>();
Map<String, Object> map = getToZipUrlMap("https://static.runoob.com/download/blockchain.pdf", "今晚打老虎","download");
Map<String, Object> map1 = getToZipUrlMap("https://static.runoob.com/download/blockchain.pdf", "一二三四五","download"+SLASH+"test");
sourceMapList.add(map);
sourceMapList.add(map1);
OutputStream sdf = new FileOutputStream("F:\\20210416.zip");
toZipHTTP(sourceMapList, sdf);
} catch (Exception e) {
e.printStackTrace();
}
}
}