java生成zip包兼容Linux
/*
这个方法只用在windows中用服务器为Linux就不行
*/
package common.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
/** 压缩单个文件*/
public static void zipFile(String filepath ,String zippath) {
//String s = UUID.randomUUID().toString();为了怕后面删除这个临时文件是删除到同一个文件
//这里filepath为要打包的文件夹的路径String filepath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/");
//zippath为生成的zip包的路径String zippath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/week-job.zip");这里生成的就是个week-job.zip包
//ServletActionContext.getServletContext().getRealPath();获得服务器地址
InputStream input = null;
ZipOutputStream zipOut = null;
try {
File file = new File(filepath);
File zipFile = new File(zippath);
input = new FileInputStream(file);
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
int temp = 0;
while((temp = input.read()) != -1){
zipOut.write(temp);
}
System.out.println("zip "+filepath+" to "+zippath);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
input.close();
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/** 一次性压缩多个文件,文件存放至一个文件夹中*/
public static void zipMultiFile(String filepath ,String zippath) {
try {
File file = new File(filepath);// 要被压缩的文件夹
File zipFile = new File(zippath);
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
if(file.isDirectory()){
File[] files = file.listFiles();
for(int i = 0; i < files.length; ++i){
input = new FileInputStream(files[i]);
zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
int temp = 0;
while((temp = input.read()) != -1){
zipOut.write(temp);
}
input.close();
}
}else{//否则,则调用压缩单个文件的方法
zipFile(filepath, zippath);
}
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------------------------------------------------------华丽的分割线----------------------------------------------------------
/*
兼容Linux的压缩方法
*/
/*
* Copyright 2017 BangChen Information Technology Ltd., Co.
* Licensed under the Apache License 2.0.
*/
package common.util;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
/**
* 压缩工具类,依赖zip4j
*
* @author L.X <gugia@qq.com>
*/
public class CompressUtils {
/**
* 解压加密的压缩文件
*
* @param zipfile
* @param dest
* @param passwd
* @throws ZipException
*/
public void unZip(File zipfile, String dest, String passwd) throws ZipException {
ZipFile zfile = new ZipFile(zipfile);
// zfile.setFileNameCharset("GBK");//在GBK系统中需要设置
if (!zfile.isValidZipFile()) {
throw new ZipException("压缩文件不合法,可能已经损坏!");
}
File file = new File(dest);
if (file.isDirectory() && !file.exists()) {
file.mkdirs();
}
if (zfile.isEncrypted()) {
zfile.setPassword(passwd.toCharArray());
}
zfile.extractAll(dest);
}
/**
* 压缩文件且加密
*
* @param src
* @param dest
* @param isCreateDir
* @param passwd
*/
public void zip(String src, String dest, boolean isCreateDir, String passwd) {
File srcfile = new File(src);
//创建目标文件
String destname = buildDestFileName(srcfile, dest);
ZipParameters para = new ZipParameters();
para.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
para.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
if (passwd != null) {
para.setEncryptFiles(true);
para.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
para.setPassword(passwd.toCharArray());
}
try {
ZipFile zipfile = new ZipFile(destname);
if (srcfile.isDirectory()) {
if (!isCreateDir) {
File[] listFiles = srcfile.listFiles();
ArrayList<File> temp = new ArrayList<>();
Collections.addAll(temp, listFiles);
zipfile.addFiles(temp, para);
}
zipfile.addFolder(srcfile, para);
} else {
zipfile.addFile(srcfile, para);
}
} catch (ZipException ex) {
System.out.println(ex.getMessage());
}
}
/**
* 目标文件名称
*
* @param srcfile
* @param dest
* @return
*/
public String buildDestFileName(File srcfile, String dest) {
if (dest == null) {//没有给出目标路径时
if (srcfile.isDirectory()) {
dest = srcfile.getParent() + File.separator + srcfile.getName() + ".zip";
} else {
String filename = srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
dest = srcfile.getParent() + File.separator + filename + ".zip";
}
} else {
createPath(dest);//路径的创建
if (dest.endsWith(File.separator)) {
String filename;
if (srcfile.isDirectory()) {
filename = srcfile.getName();
} else {
filename = srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
}
dest += filename + ".zip";
}
}
return dest;
}
/**
* 路径创建
*
* @param dest
*/
private void createPath(String dest) {
File destDir;
if (dest.endsWith(File.separator)) {
destDir = new File(dest);//给出的是路径时
} else {
destDir = new File(dest.substring(0, dest.lastIndexOf(File.separator)));
}
if (!destDir.exists()) {
destDir.mkdirs();
}
}
}
调用
CompressUtils compressUtils = new CompressUtils();
compressUtils.zip(zippaths[0], zipFilePath, true, null);
java生成zip包兼容Linux的更多相关文章
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- java 生成jar包并保留注释
java 生成jar包并保留注释 CreationTime--2018年7月17日08点32分 Author:Marydon 1.选中java项目-->右键-->Export: 2.去 ...
- 将java打jar包成linux后台服务service
将java打jar包成linux后台服务service 第一步:将java程序打成jar包 build.gradle配置文件中加spring-boot-gradle-plugin插件,具体配置如下(配 ...
- java生成jar包
Java编写的application程序是否可以终于形成一个类似于exe一样的可执行文件.难道就仅仅能用命令行执行? 通常有两种.一种是制作一个可运行的JAR文件包.然后就能够像.chm文档一样双击运 ...
- springboot jar文件打zip包运行linux环境中
1.添加打包配置文件 1.1 assembly.xml <assembly xmlns="http://maven.apache.org/plugins/maven-assembly ...
- java生成zip压缩文件,解压缩文件
1.生成zip public static void main(String[] args) { try { // testZip("c:\\temp.txt", "c: ...
- java 生成zip文件并导出
总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...
- java中构建同时兼容linux和windows程序时遇到的文件路径分割符问题解决方案
最近在做一个自动上传文件的客户端,因为 file.getAbsolutePath() 在Mac和linux下的分割符是“/”,而在windows操作系统下的分割符则是“\”,我们程序中固然可以通过调 ...
- Java一键部署包,Linux部署不用愁!!!
前言 昨天一哥们的弟弟突然问我有没有部署过的Linux,公司连个运维都没有,服务器都要后端部署.... 你有没有相似的遭遇呢?公司规模小,后端即是运维,一份工资干两份活,哈哈~ 为了解决这老弟的困惑, ...
随机推荐
- (一)easyUI之第一个demo
一.下载 官网下载 : http://www.jeasyui.net/download/ 同时并下载官方中文API文档. 解压后的目录结构: 二.第一个demo 1 新建工程并导入包 2 ...
- Java数据结构ArrayList
Java数据结构ArrayList /** * <html> * <body> * <P> Copyright JasonInternational</p&g ...
- C# 中类的成员有哪些?
类(class)是C#类型中最基础的类型.类是一个数据结构,将状态(字段)和行为(方法和其他函数成员)组合在一个单元中.类提供了用于动态创建类实例的定义,也就是对象(object).类支持继承(inh ...
- ZROI17普及23-B星空题解--图的灵活转化
题目链接 版权原因不予提供 分析 这题思路很妙啊,虽然已经算半个套路题(因为我太菜了) 将框视为点,若一个球能放在\(x\)或\(y\)框,则\(x,y\)连一条无向边.有一条非常显然的性质是:在联通 ...
- pytorch中使用多显卡训练以及训练时报错:expect more than 1 value per channel when training, got input size..
pytorch在训练中使用多卡: conf.device = torch.device('cuda:0' if torch.cuda.is_available() else "cpu&quo ...
- vue之双向绑定
Vue的一大核心是双向绑定,在2.0中采用数据劫持,用Object.defineProperty实现,但作者已声明在3.0中会采用proxy实现 Object.defineProperty是什么? ...
- 在Linux下执行Jmeter脚本
前言 Jmeter这款接口测试工具,已经在越来越多的公司被要求会使用了. 而且,现在应该部分小伙伴们都开始用起来了. 但是,你们知道除了在Windows用图形化界面的Jmeter执行脚本之外,还有其他 ...
- zabbix-通过自动发现添加主机
当生产环境中需要监控海量的机器的时候,特别是像58.赶集这类同城性质的大网站,或者京东.阿里云这样的造节电商,每次活动.大促都需要添加很多机器来应对海量用户流量,每天都有可能上架新的机器.或者添加新的 ...
- CentOS7安装CDH 第五章:CDH的安装和部署-CDH5.7.0
相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...
- 关于在window8上使用ssh命令的记录
1.开启虚拟机以及git bash窗口,准备连接 2.在虚拟机中输入ifconfig -a查看虚拟机ip 从图中找到ip为 : inet 地址:192.168.78.133 3.输入命令: ssh r ...