import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
private static Logger logger = LoggerFactory.getLogger(ZipUtil.class); public static void zipFile(InputStream fileStream, String fileName, ZipOutputStream zipOutputStream, HashMap index) throws IOException { logger.info("开始压缩文件--------------------------------------"); ZipEntry entry = new ZipEntry(fileName); if (index.containsKey(fileName)){ //遇到想同房文件名 重命名 int i= (Integer) index.get(fileName); int i1 = fileName.lastIndexOf('.'); String name = fileName.substring(0, i1); String type = fileName.substring(i1); zipOutputStream.putNextEntry(new ZipEntry( name+"(" + i + ")"+type)); index.put(fileName,i+1); } else { zipOutputStream.putNextEntry(entry); index.put(fileName,1); } byte [] content=new byte[1024]; int len; while((len=fileStream.read(content))!=-1){ zipOutputStream.write(content,0,len); zipOutputStream.flush(); } // 关闭文件入流 fileStream.close(); }
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)