Java 实现生成外部文件zip 和 解压zip
话不多说,先上jar
<!-- ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
我这用的是pom.xml的配置,不是的可以直接下载2个jar放入到项目中
下面直接上代码
/**
* 生成zip文件
* Title: saveToDescription
* Description:
* @author Zhangjl
* @param Description
* @param UID
* @param shop
* @param itemid
* @return
*/
public static String saveToDescription(String Description, String UID, String shop, String itemid) {
//本地地址
String startpath = "c:\\description\\";
//网络访问地址
String ipPath = "http://localhost:8080/description";
File f = null;
String fileName = "";
String path = startpath + File.separator + UID + File.separator + shop;
path = path.replace("/", "_").replace(" ", "_");
fileName = itemid;
f = new File(path);
if (!f.exists()) {
if (!f.mkdirs()) {
System.out.println("不能创建目录:" + path);
return "";
}
}
File file = new File(path, fileName + ".zip");
try (ZipOutputStream zos = new ZipOutputStream(file)) {
zos.putNextEntry(new ZipEntry(fileName + ".txt"));
zos.write(Description.getBytes(Charsets.UTF_8));
zos.closeEntry();
zos.flush();
String url = file.getAbsolutePath();
url = url.replace(startpath, ipPath).replace("\\", "/");
return url;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static boolean isNullOrEmpty(Object obj) {
return obj == null || "".equals(obj.toString());
}
/**
* 解压读取zip的内容
* Title: readDescribeFromFile
* Description:
* @author Zhangjl
* @param descriptionURL
* @return
*/
public static String readDescribeFromFile(String descriptionURL) {
if (!isNullOrEmpty(descriptionURL)) {
String tempFile = "";
if (descriptionURL.contains("向易"))
descriptionURL = descriptionURL.replace("向易", "%E5%90%91%E6%98%93");
// 读取描述文件的代码
try {
if (!isNullOrEmpty(descriptionURL)) {
URL url = new URL(descriptionURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3 * 1000);
// 得到输入流
InputStream inputStream = conn.getInputStream();
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
BufferedInputStream bs = new BufferedInputStream(zipInputStream);
java.util.zip.ZipEntry nextEntry;
nextEntry = zipInputStream.getNextEntry();
while (nextEntry != null) {
tempFile = nextEntry.getName();
File file = new File(tempFile);
// 如果是目录,创建目录
if (tempFile.endsWith("/")) {
file.mkdir();
} else {
// 文件则写入具体的路径中
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int n;
byte[] bytes = new byte[1024];
while ((n = zipInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, n);
}
// 关闭流
bufferedOutputStream.close();
fileOutputStream.close();
}
// 关闭当前布姆
zipInputStream.closeEntry();
// 读取下一个目录,作为循环条件
nextEntry = zipInputStream.getNextEntry();
}
String returnContent = readFile(tempFile);
deleteFile(tempFile);
return returnContent;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
public static String readFile(String fileName) {
// custName = new String(custName.getBytes("ISO-8859-1"), "UTF-8");
String fileContent = "";
try {
File f = new File(fileName);
if (f.isFile() && f.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(f), "UTF-8");
BufferedReader reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
fileContent += line;
fileContent += "\\n";
}
read.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return fileContent;
}
public static boolean deleteFile(String sPath) {
Boolean flag = false;
File file = new File(sPath);
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
这里面一个是压缩并生成zip文件,返回访问路径
另一个是解压读取里面的内容