使用java实现zip文件的解压

最近,使用到了对zip文件进行解压,顺便整理了一下对文件流的处理,一起梳理一下,方便日后使用

首先来说对文件进行读取和复制是常见操作,先看一下一个简单实现

public static void copy(){
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(frompathname));//frompathname为需要复制的文件路径
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(topathname));//topathname为复制到的文件路径,目标文件应当与源文件格式一致,例 //如源文件为txt,则目标文件也应当为txt。例如formpathname=C:/aa.txt ,topathname=C:/bb.txt
            byte[]  buffer  =  new  byte[1444];  
            int b;
            while((b=bis.read(buffer))>-1){
                bos.write(buffer,0,b);
            }
            bos.close();
            bis.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }    

对于应当使用字符流还是字节流进行读写操作的问题 ,如果为文本文件,则可以使用字符流进行读写,但是如果为图片等文件 则应当使用字节流进行读写,而且读写时候应当保持一致,即要么同时为字符流,要么同时为字节流

如果是zip文件直接复制同上类似,如果需要进行解压,则代码如下


public static void unzip() {

        try {

           /**此段代码不需要
            BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(new File("C:/3.7.zip")));
            int b ;
            byte[] tempbyte = new byte[8];
            byte[] bytes = null;
            byte[] temp = null;

           try {
                while ((b=bis.read(tempbyte, 0, tempbyte.length)) >-1) {
                    if(bytes==null){
                        bytes = new byte[tempbyte.length];
                        System.arraycopy(tempbyte, 0, bytes, 0, tempbyte.length);
                    }else{
                        temp = new byte[bytes.length+tempbyte.length];
                        System.arraycopy(bytes, 0, temp, 0, bytes.length);
                        System.arraycopy(tempbyte, 0, temp, bytes.length, tempbyte.length);
                        bytes = temp;
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            InputStream ips = new ByteArrayInputStream(bytes);

           */

            FileInputStream fis = new FileInputStream("C:/aa.zip");
            ZipInputStream zis = new ZipInputStream(bis);//一定需要zipInputstream这个zip字节流,这个是关键,否则会当成一个文件处理
            BufferedInputStream bbis = new BufferedInputStream(zis);
            File Fout = null;
            ZipEntry entry;
            String Parent = "D:\\Desktop"; // 输出路径(文件夹目录)
            try {
                entry = zis.getNextEntry();
                System.out.println("entry" + entry);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                while ((entry = zis.getNextEntry()) != null
                        && !entry.isDirectory()) {
                    Fout = new File(Parent, entry.getName());
                    if (!Fout.exists()) {
                        (new File(Fout.getParent())).mkdirs();
                    }
                    FileOutputStream out = new FileOutputStream(Fout);
                    BufferedOutputStream Bout = new BufferedOutputStream(out);
                    int bb;
                    while ((bb = bbis.read()) != -1) {
                        Bout.write(bb);
                    }
                    Bout.close();
                    out.close();
                    System.out.println(Fout + "解压成功");
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // System.out.println(str);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

简单总结,便于以后使用



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值