java zip压缩慢_Java开发之浅谈ZIP压缩中要注意的几点

前言

ZIP,是一个文件的压缩的算法。ZIP通常使用后缀名“.zip”,它的MIME格式为 application/zip 。目前,ZIP格式属于几种主流的压缩格式之一,其竞争者包括RAR格式以及开放源码的7-Zip格式。从性能上比较,RAR格式较ZIP格式压缩率较高,但是它的压缩时间远远高于Zip。

ec21ae9da6ce5ddb7eb8c3bf36e2e1f1.png

在日常java开发中,经常会用到将一个文件夹或文件夹中的内容压缩成一个zip包,这里我们就从以下几个需要注意的事项入手,谈一谈java如何开发zip压缩类。

zip压缩注意事项

1 判断当前服务器是Windows服务器还是Linux服务器

我们知道,当前服务器的操作系统的两大主流是windows服务器和linux服务器,对于不同的服务器,zip压缩软件的安装路径可能不同,如Windows上可能配有winrar,而winrar会极大的提高压缩效果;而linux服务器呢,可能会安装有zip而可以使用zip命令压缩,也比java自有的类速度要快很多。那么如何区分当前系统是Windows系统还是Linux系统呢?

Properties props=System.getProperties();

if(props.getProperty("os.name").indexOf("Linux")>-1){

//这里可以执行Linux里面的操作

}else if(props.getProperty("os.name").indexOf("Windows")>-1){

//这里可以执行Linux里面的操作

}

2.Windows系统下如何调用winrar压缩软件进行zip压缩

729cd32c58daa2aab5185a5caf5ea5c8.png

public static boolean winrar(String winrarfile, String folder) {

String rarPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";

File winrarFile=new File(rarPath);

if(winrarFile.isFile()&&winrarFile.exists()){

String cmd="";

cmd = rarPath + " a -ep1 " + winrarfile + " "+ folder;

try {

Process proc = Runtime.getRuntime().exec(cmd);

if (proc.waitFor() != 0) {

if (proc.exitValue() == 0)

return true;

}

} catch (Exception e) {

e.printStackTrace();

}

}

return false;

}

3.Linux下调用zip命令进行zip压缩

该处需要注意,首先应在Linux服务器上安装zip,可以使用命令:yum -y install zip;

public static boolean linuxZip(String zipfile, String folder){

try {

File file=new File(folder);

if(!file.exists())

{

return false;

}

if(file.isDirectory()&&file.listFiles().length==0)

{

return false;

}

Process proc = Runtime.getRuntime().exec(new String[] { "/bin/csh", "-c","cd "+folder+";zip -r "+zipfile +" ./*" });

if (proc.waitFor() != 0) {

if (proc.exitValue() == 0)

return true;

}

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

4.注意压缩采用的编码,避免乱码现象

ZipOutputStream zos = new ZipOutputStream(new File(zipFileName));

zos.setEncoding("gb2312");

这里如果压缩后文件夹内中文文件名出现乱码,尝试修改gb2312为utf-8等别的编码,可有效解决乱码问题。

结语

如果您在开发过程中遇到有关zip的压缩问题,不妨在下方留言,大家一起来应对助您解决问题。如果感觉本文对您有帮助,请收藏并转发。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中使用Zip压缩中文文件名可能会出现乱码的问题,这是因为Zip文件格式默认使用的是ASCII编码,而中文字符需要使用其他编码方式进行处理。解决这个问题的方法有两种: 1. 使用Apache Commons Compress库 可以使用Apache Commons Compress库来压缩文件,这个库支持多种编码方式,可以解决中文文件名乱码的问题。示例代码如下: ```java import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; public class ZipUtil { public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipArchiveOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); InputStream is = new FileInputStream(file); IOUtils.copy(is, zos); is.close(); zos.closeArchiveEntry(); } } } ``` 2. 使用JavaZipOutputStream类 Java中自带的ZipOutputStream类也可以用于压缩文件,但是需要手动设置编码方式,示例代码如下: ```java import java.io.*; import java.nio.charset.Charset; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtil { private static final int BUFFER_SIZE = 2 * 1024; public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipOutputStream zos = new ZipOutputStream(fos, Charset.forName("GBK")); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); zos.closeEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); InputStream is = new FileInputStream(file); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = is.read(buffer)) != -1) { zos.write(buffer, 0, len); } is.close(); zos.closeEntry(); } } } ``` 以上两种方法都可以解决中文文件名乱码的问题,具体使用哪一种方法可以根据实际需要选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值