匆匆那唸 2015-07-29 03:53 采纳率: 0%
浏览 1535
已采纳

java 打包压缩下载出错,求大神帮忙

代码在这里
package cn.mobilizer.channel.image.vo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipFile;

import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import cn.mobilizer.channel.image.po.Image;
import cn.mobilizer.channel.util.PropertiesHelper;

public class ZipSupport {

protected static Logger log = Logger.getLogger(ZipSupport.class);

private static ZipSupport  zipSupport = null;
private static String imageFolder = null;

static{
    imageFolder = PropertiesHelper.getInstance().getProperty(PropertiesHelper.IMG_FOLDER);
}

private ZipSupport(){

}

public static ZipSupport getInstance(){
    if(null==zipSupport){
        zipSupport = new ZipSupport();
    }
    return zipSupport;
}


/**
 * 根据文件名,压缩文件
 * 
 * @param fileNames 需要压缩的索引文件名
 * @param zipFileName 压缩后的文件名
 * @param out
 * @throws Exception
 */
public void exportFileByName(List<ImageView> images, String zipFileName,OutputStream out) throws Exception {
    if(null!=images && !images.isEmpty()){
        zipFileByName(images, zipFileName, out);
    }
}



/**
 * 根据文件夹,压缩
 * @param folder 需要压缩的文件夹
 * @param zipFileName 压缩后的文件名
 * @param out
 * @throws Exception
 */
public void exportFileByFolder(String folder,OutputStream out) throws Exception{
    zipFolder(folder,out);
}

/**
 * 根据文件名称获取文件存储路径
 * @param fileNames
 * @return
 */
public List<File> getFileByName(Set<String> fileNames) {
    List<File> files = new ArrayList<File>();
    String imageFolder = PropertiesHelper.getInstance().getProperty(PropertiesHelper.FTP_FOLDER);
    log.debug("imageFolder:" + imageFolder);

    for (String name : fileNames) {
        String[] temp = name.split("_");
        String clientId = temp[1];
        String userId = temp[2];
        StringBuffer path = new StringBuffer();
        path.append(imageFolder).append(File.separator).append(clientId).append(File.separator).append(userId).append(File.separator).append(name);
        File file = new File(path.toString());
        files.add(file);
    }
    return files;
}

private void zipFileByName(List<ImageView> images,String zipFileName, OutputStream out) throws Exception {
    InputStream input = null;
    ZipOutputStream zipOut = new ZipOutputStream(out);
    try {
        for (ImageView view : images) {
            List<Image> imges = view.getImages();
            for (Image image : imges) {
                input = new FileInputStream(imageFolder+File.separator+image.getLargeImagepath());
                String entry = zipFileName + File.separator+ image.getStoreName()+ File.separator + view.getShowDate() + File.separator+ image.getLargeImagepath();
                zipOut.putNextEntry(new ZipEntry(entry));
                int temp = 0;
                while ((temp = input.read()) != -1) {
                    zipOut.write(temp);
                }
                zipOut.setEncoding("UTF-8");
            }
        }
    } finally {
        if (null != input) {
            input.close();
        }
        zipOut.flush();
        zipOut.close();
    }
}



public void zipFileByNameColgate(Map<String,List<ColgateImageVo>> mapImages,String zipFileName,String cycle, OutputStream out) throws Exception {
    ZipOutputStream zipOut = new ZipOutputStream(out);
    zipOut.setEncoding("UTF-8");
    byte[] buffer = new byte[2048];
    StringBuffer base = new StringBuffer();
    base.append(zipFileName).append(File.separator).append(cycle).append(File.separator);

    try {
        Set<String> keys = mapImages.keySet();
        for (String key : keys) {
            List<ColgateImageVo> imges = mapImages.get(key);                
            for (int i = 0; i < imges.size(); i++) {
                ColgateImageVo image = imges.get(i);
                String dir = imageFolder+File.separator+image.getLargeImagepath();
                log.info("dir:"+dir);
                InputStream input = new FileInputStream(dir);
                String suffix = image.getLargeImagepath().substring(image.getLargeImagepath().lastIndexOf("."),image.getLargeImagepath().length());
                base.append(image.getStoreName()).append(File.separator).append(image.getImageShowName()).append("_").append(i).append(suffix);
                log.info("base:"+base);
                ZipEntry entry = new ZipEntry(base.toString());
                zipOut.putNextEntry(entry);
                int num;
                while((num = input.read()) != -1){
                    zipOut.write(buffer,0,num);
                }
                input.close();
            }
        }
        zipOut.flush();
        out.flush();
    } finally {
        zipOut.closeEntry();
        zipOut.close();
        out.close();
    }
}


/**
 * 压缩文件为zip格式
 * 
 * @param output
 *            ZipOutputStream对象
 * @param file
 *            要压缩的文件或文件夹
 * @param basePath
 *            条目根目录
 * @throws IOException
 */
private void zipFile(ZipOutputStream output, File file,String basePath) throws IOException {
    FileInputStream input = null;
    try {
        // 文件为目录
        if (file.isDirectory()) {
            // 得到当前目录里面的文件列表
            File list[] = file.listFiles();
            basePath = basePath + (basePath.length() == 0 ? "" : File.separator)+ file.getName();
            // 循环递归压缩每个文件
            for (File f : list){
                zipFile(output, f, basePath);
            }
        } else {
            // 压缩文件
            basePath = (basePath.length() == 0 ? "" : basePath + File.separator) + file.getName();
            output.putNextEntry(new ZipEntry(basePath));
            input = new FileInputStream(file);
            int readLen = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {
                output.write(buffer, 0, readLen);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        // 关闭流
        if (input != null) {
            input.close();
        }
    }
}

private void zipFolder(String zipFolder,OutputStream out) throws Exception {
    ZipOutputStream output = null;
    try {
        File file = new File(zipFolder);
        output = new ZipOutputStream(out);
        // 顶层目录开始
        zipFile(output, file, "");
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        // 关闭流
        if (output != null) {
            output.flush();
            output.close();
        }
    }
}

}
整个压缩包就几十K的东西,里面图片变成8.5M的,且错误

  • 写回答

4条回答 默认 最新

  • JonsonJiao 2015-07-29 04:38
    关注

    这句里面有问题吧。buffer没有值。

    int num;
                    while((num = input.read()) != -1){
                        zipOut.write(buffer,0,num);
                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已采纳回答 9月23日

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境