java web zip流下载

使用zip边压缩边下载

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipHttpInputStream extends InputStream {
    
    private List<File>            list            = new ArrayList<>();
    
    private ByteArrayOutputStream bos             = new ByteArrayOutputStream();
    private ZipOutputStream       zipOutputStream = new ZipOutputStream(bos);
    private FileInputStream       fileInputStream = null;
    
    private int                   fileIndex       = 0;
    private boolean               readFinish      = false;
    private byte[]                curByte         = new byte[0];
    private int                   readPos         = 0;
    
    public ZipHttpInputStream(List<File> list) {
        this.list = list;
        nextFile();
    }
    
    public boolean readFile() throws IOException {
        
        if (readFinish) {
            return false;
        }
        byte[] buffer = new byte[1024 * 10];
        int    len    = fileInputStream.read(buffer);
        if (len < 0) {
            if (fileIndex >= list.size()) {
                zipOutputStream.closeEntry();
                zipOutputStream.close();
                curByte    = bos.toByteArray();
                readFinish = true;
                return true;
            }
            
            nextFile();
            
            len = fileInputStream.read(buffer);
        }
        zipOutputStream.write(buffer, 0, len);
        curByte = bos.toByteArray();
        bos.reset();
        return true;
    }
    
    public void nextFile() {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            
            File file = list.get(fileIndex);
            fileIndex++;
            fileInputStream = new FileInputStream(file);
            ZipEntry zipEntry = new ZipEntry(file.getName());
            zipOutputStream.putNextEntry(zipEntry);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public int read() throws IOException {
        return -1;
    }
    
    @Override
    public int read(byte b[], int off, int len) throws IOException {
        Objects.checkFromIndexSize(off, len, b.length);
        int count = curByte.length;
        
        if (readPos >= count) {
            
            if (!readFile()) {
                return -1;
            }
            readPos = 0;
            count   = curByte.length;
        }
        
        int avail = count - readPos;
        if (len > avail) {
            len = avail;
        }
        if (len <= 0) {
            return 0;
        }
        System.arraycopy(curByte, readPos, b, off, len);
        readPos += len;
        return len;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值