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

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);
                    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog