700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java 解压rar 解压缩zip

Java 解压rar 解压缩zip

时间:2019-07-24 10:13:08

相关推荐

Java 解压rar 解压缩zip

首先是MAVEN的pom文件中添加 junrar 找了一天多 没找到jdk中能用的RAR解压类。原因是RAR是有专利的。

不说了---show me code!

<!-- /artifact/com.github.junrar/junrar --><dependency><groupId>com.github.junrar</groupId><artifactId>junrar</artifactId><version>0.7</version></dependency>

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Date;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;import com.github.junrar.Archive;import com.github.junrar.exception.RarException;import com.github.junrar.exception.RarException.RarExceptionType;import com.github.junrar.rarfile.FileHeader;/*** @author zhuss* @time -09-07*/public class ZipUtil {/*** @param zipOutFile* The file will be generated* @param zipInputFile* To compress the file or folder* @throws IOException*/public static boolean zipCompressing(File zipOutFile, File zipInputFile)throws IOException {boolean flag = true;ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipOutFile));BufferedOutputStream bos = new BufferedOutputStream(zos);flag = zipCompressingStream(zos, zipInputFile, zipOutFile.getAbsolutePath(),bos);bos.close();zos.close();return flag;}/*** @param zipFileName* The name of the zip file will be generated* @param zipInputFile* To compress the file or folder* @throws IOException*/public static boolean zipCompressing(String zipFileName, String zipInputFile)throws IOException {return zipCompressing(new File(zipFileName), new File(zipInputFile));}/*** * @param out* The implementation of file compression flow* @param f* To compress the file or folder* @param base* Root node of the compressed file* @param bos* The compression stream buffer* @throws IOException*/public static boolean zipCompressingStream(ZipOutputStream out, File f,String base, BufferedOutputStream bos) throws IOException {boolean flag = true;if (f.isDirectory()) {File[] f1 = f.listFiles();if (f1.length == 0) {out.putNextEntry(new ZipEntry(base + "/"));} else {for (int i = 0; i < f1.length; i++) {File file = f1[i];flag = flag && zipCompressingStream(out, file, file.getName(), bos);}}} else {out.putNextEntry(new ZipEntry(base));FileInputStream in = new FileInputStream(f);BufferedInputStream bis = new BufferedInputStream(in);int b;while ((b = bis.read()) != -1) {bos.write(b);}bos.flush();bis.close();in.close();flag = true;}return flag;}/*** * @param zipFile* To unzip the files* @param outputPath* Extract the location*/public static void zipDecompressing(File zipFile, String outputPath)throws IOException {ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));BufferedInputStream bis = new BufferedInputStream(zis);File fOut = null;ZipEntry entry = null;while ((entry = zis.getNextEntry()) != null && !entry.isDirectory()) {fOut = new File(outputPath, entry.getName());if (!fOut.exists()) {(new File(fOut.getParent())).mkdirs();}FileOutputStream fos = new FileOutputStream(fOut);BufferedOutputStream bos = new BufferedOutputStream(fos);int b;while ((b = bis.read()) != -1) {bos.write(b);}bos.close();fos.close();}bis.close();zis.close();}/*** @param rarFileName rar file name* @param outFilePath output file path * @return success Or Failed* @author zhuss* @throws Exception*/public static boolean unrar(String rarFileName, String outFilePath) throws Exception{ boolean flag = false;try { Archive archive = new Archive(new File(rarFileName)); if(archive == null){throw new FileNotFoundException(rarFileName + " NOT FOUND!");}if(archive.isEncrypted()){throw new Exception(rarFileName + " IS ENCRYPTED!");}List<FileHeader> files = archive.getFileHeaders();for (FileHeader fh : files) {if(fh.isEncrypted()){throw new Exception(rarFileName + " IS ENCRYPTED!");} String fileName = fh.getFileNameW();if(fileName != null && fileName.trim().length() > 0){String saveFileName = outFilePath+"\\"+fileName;File saveFile = new File(saveFileName);File parent = saveFile.getParentFile();if(!parent.exists()){parent.mkdirs();}if(!saveFile.exists()){saveFile.createNewFile();}FileOutputStream fos = new FileOutputStream(saveFile);try { archive.extractFile(fh, fos); fos.flush();fos.close();} catch (RarException e) { if(e.getType().equals(RarExceptionType.notImplementedYet)){ } }finally{ } }}flag = true;} catch (Exception e) {e.printStackTrace();throw e;} return flag;}}

大家有什么好的RAR压缩和解压缩的类,欢迎推荐给我!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。