java对zip文件进行解压并取出文件的名称

1、先用JDK的类进行ZIP文件解压

    public List releaseWinZIPByList(String zipPath, String serverPath) {

        //zipPath为需要解压的zip文件
        //serverPath为解压后文件的存放路径
        List list = null;
        ZipInputStream in = null;
        FileOutputStream os = null;
        try {
            in = new ZipInputStream(new FileInputStream(zipPath));
            ZipEntry entry = null;
            while ((entry = in.getNextEntry()) != null) {
                String entryName = entry.getName();
                if (entry.isDirectory()) {
                    File file = new File(serverPath + File.separator + entryName);
                    file.mkdirs();
                } else {
                    os = new FileOutputStream(serverPath + File.separator + entryName);
                    int bytesRead = 0;
                    while ((bytesRead = in.read()) != -1) {
                        os.write(bytesRead);
                    }
                }// 未取值
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Log4J.logWarn(e.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log4J.logWarn(e.getMessage());
        }
        return list;
    }

    JDK的解压的字符集只支持ISO-8859-1,所以说在解压的时候出现中文的地方会出现异常,可以采取先把文件转换成UUID的编码名称,当然这是没有业务需求的情况下!

2、利用ant解压文件

   public static void releaseZipFileList(String zipPath, String serverPath)
            throws Exception {
        //zipPath为需要解压的zip文件
        //serverPath为解压后文件的存放路径
        ZipFile zipFile = new ZipFile(zipPath);
        Enumeration e = zipFile.getEntries();
        org.apache.tools.zip.ZipEntry zipEntry = null;
        while (e.hasMoreElements()) {
            zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
            String entryName = zipEntry.getName();
            String fileNames[] = entryName.split("/");//读取文件集合
            int length = fileNames.length;
            String path = serverPath;
            for (int it = 0; it < length; it++) {
                if (it < length - 1) {
                    path += fileNames[it] + "/";
                    new File(path).mkdir();
                } else { // 读取最后的一个文件
                    if (entryName.endsWith("/")) { // 为目录,则创建文件夹
                        new File(serverPath + entryName).mkdir();
                    } else {
                        InputStream in = zipFile.getInputStream(zipEntry);
                        OutputStream os = new FileOutputStream(new File(
                                serverPath + entryName));
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            os.write(buf, 0, len);
                        }
                        in.close();
                        os.close();
                    }
                }
            }
        }
        zipFile.close();
    }

利用ant挤压可以支持中文,但是效率不高,有需要的时候可以用,用到的类在ant包中

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现zip文件上传并解压的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet("/upload") @MultipartConfig // 必须加上此注解才能处理文件上传 public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取上传的文件 Part filePart = request.getPart("file"); // 获取文件名 String fileName = filePart.getSubmittedFileName(); // 获取文件输入流 FileInputStream fileInputStream = (FileInputStream) filePart.getInputStream(); // 创建目标文件 File targetFile = new File(fileName); FileOutputStream fileOutputStream = new FileOutputStream(targetFile); // 将上传的文件写入到目标文件中 byte[] buffer = new byte[1024]; int len; while ((len = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); } fileInputStream.close(); fileOutputStream.close(); // 解压文件 unzip(targetFile); // 输出成功信息 response.getWriter().println("文件上传并解压成功!"); } private void unzip(File zipFile) throws IOException { // 创建解压目录 String unzipDir = zipFile.getName().substring(0, zipFile.getName().lastIndexOf('.')); File unzipDirFile = new File(unzipDir); unzipDirFile.mkdirs(); // 创建ZipInputStream ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile)); // 循环读取ZipEntry ZipEntry zipEntry = zipInputStream.getNextEntry(); while (zipEntry != null) { String entryName = zipEntry.getName(); String entryPath = unzipDir + File.separator + entryName; if (zipEntry.isDirectory()) { // 如果是目录,则创建目录 new File(entryPath).mkdirs(); } else { // 如果是文件,则写入文件 FileOutputStream fileOutputStream = new FileOutputStream(entryPath); byte[] buffer = new byte[1024]; int len; while ((len = zipInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); } fileOutputStream.close(); } zipEntry = zipInputStream.getNextEntry(); } zipInputStream.closeEntry(); zipInputStream.close(); } } ``` 该代码使用了Servlet 3.0规范的`@MultipartConfig`注解来处理文件上传,通过`request.getPart("file")`方法获取上传的文件。然后将文件写入到目标文件中,并调用`unzip()`方法解压文件。`unzip()`方法使用`ZipInputStream`读取zip文件中的每个`ZipEntry`,如果是目录则创建目录,如果是文件则写入文件解压完成后,输出成功信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值