cnblogs/dcrenl/springboot 中 java.util.zip 文件的压缩.html

111 lines
9.6 KiB
HTML
Raw Normal View History

2024-09-24 12:43:01 +08:00
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.geotmt.billingcenter.common.utils;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.datanucleus.util.StringUtils;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.slf4j.Logger;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.slf4j.LoggerFactory;
</span><span style="color: #0000ff;">import</span> java.io.*<span style="color: #000000;">;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.zip.CRC32;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.zip.CheckedOutputStream;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.zip.ZipEntry;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.zip.ZipOutputStream;
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* @Description 压缩与解压工具
* @Author yanghanwei
* @Date 18:42 2019-11-20
* @Version v1
*</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> ZipUtils {
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> Logger logger = LoggerFactory.getLogger(ZipUtils.<span style="color: #0000ff;">class</span><span style="color: #000000;">);
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 压缩 zip
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> filePath 文件夹 全路径
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> fileName 文件夹名称
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> outPath 压缩文件保存路径
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> zipFile(String filePath, String fileName, String outPath) {
logger.info(</span>"filePath:{}, fileName:{}, outPath:{}"<span style="color: #000000;">, filePath, fileName, outPath);
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;">创建Test.zip文件</span>
OutputStream is = <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileOutputStream(outPath);
</span><span style="color: #008000;">//</span><span style="color: #008000;">检查输出流,采用CRC32算法保证文件的一致性</span>
CheckedOutputStream cos = <span style="color: #0000ff;">new</span> CheckedOutputStream(is, <span style="color: #0000ff;">new</span><span style="color: #000000;"> CRC32());
</span><span style="color: #008000;">//</span><span style="color: #008000;">创建zip文件的输出流</span>
ZipOutputStream zos = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ZipOutputStream(cos);
</span><span style="color: #008000;">//</span><span style="color: #008000;">需要压缩的文件或文件夹对象</span>
File file = <span style="color: #0000ff;">new</span><span style="color: #000000;"> File(filePath);
</span><span style="color: #008000;">//</span><span style="color: #008000;">压缩文件的具体实现函数</span>
<span style="color: #000000;"> zipFilePost(zos,file,filePath,fileName,outPath);
zos.close();
cos.close();
is.close();
System.out.println(</span>"压缩完成"<span style="color: #000000;">);
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
logger.error(</span>"压缩失败zipFileException:" +<span style="color: #000000;"> e);
}
}
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 压缩文件
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> zos zip文件的输出流
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> file 需要压缩的文件或文件夹对象
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> filePath 压缩的文件路径
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> fileName 需要压缩的文件夹名
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> outPath 缩完成后保存为Test.zip文件
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> zipFilePost(ZipOutputStream zos, File file, String filePath, String fileName, String outPath){
</span><span style="color: #0000ff;">try</span><span style="color: #000000;">{
String path </span>=<span style="color: #000000;"> file.getPath();
String zosName </span>= ""<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span>(!<span style="color: #000000;">StringUtils.isEmpty(path)){
zosName </span>=<span style="color: #000000;"> path.substring(path.indexOf(fileName));
}
File[] files </span>=<span style="color: #000000;"> file.listFiles();
</span><span style="color: #0000ff;">if</span>(file.isDirectory() &amp;&amp; files != <span style="color: #0000ff;">null</span> &amp;&amp; files.length &gt; 0<span style="color: #000000;">) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建压缩文件的目录结构</span>
zos.putNextEntry(<span style="color: #0000ff;">new</span> ZipEntry(zosName +<span style="color: #000000;"> File.separator));
</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(File f : files) {
zipFilePost(zos, f, filePath, fileName, outPath);
}
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
logger.info(</span>"正在压缩文件:{}"<span style="color: #000000;">,file.getName());
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建压缩文件</span>
zos.putNextEntry(<span style="color: #0000ff;">new</span><span style="color: #000000;"> ZipEntry(zosName));
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 用字节方式读取源文件</span>
InputStream is = <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(file.getPath());
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建一个缓存区</span>
BufferedInputStream bis = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BufferedInputStream(is);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 字节数组,每次读取1024个字节</span>
<span style="color: #0000ff;">byte</span> [] b = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1024<span style="color: #000000;">];
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 循环读取,边读边写</span>
<span style="color: #0000ff;">while</span>(bis.read(b)!=-1<span style="color: #000000;">) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 写入压缩文件</span>
<span style="color: #000000;"> zos.write(b);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">关闭流</span>
<span style="color: #000000;"> bis.close();
is.close();
}
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
logger.error(</span>"压缩文件失败zipFilePostException" +<span style="color: #000000;"> e);
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main(String[] args) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception{
String filePath </span>= "/var/folders/88/jh37h0fj59l1f302jdryz4780000gn/T/201908月小微平台消耗-1574300435525/"<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 需要压缩的文件夹名</span>
String fileName = "201908月小微平台消耗-1574300435525"<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 压缩完成后保存为Test.zip文件名字随意</span>
String outPath = "/var/folders/88/jh37h0fj59l1f302jdryz4780000gn/T/Test3.zip"<span style="color: #000000;">;
zipFile(filePath, fileName, outPath);
}
}</span></pre>
</div>
<p>&nbsp;</p>
<p>转自https://blog.csdn.net/qq_29323645/article/details/103202158</p>