您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么進(jìn)行壓縮jar,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
頁面上傳jar包
后臺解壓jar包
頁面展示所有package
選擇一個package
頁面顯示class和子package
選擇class,進(jìn)入class解析頁面
選擇package,顯示class和子package
package com.wuxiongwei.java.jar2; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * 非常好的工具類 <br> * 壓縮成jar. * @author * @version 1.0.0 */ public class JarCompressor { private static Log log = LogFactory.getLog(JarCompressor.class); private static final int BUFFER = 8192; private File fileName; private String originalUrl; public JarCompressor(String pathName) { fileName = new File(pathName); } public void compress(String... pathName) { ZipOutputStream out = null; try { FileOutputStream fileOutputStream = new FileOutputStream(fileName); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); out = new ZipOutputStream(cos); String basedir = ""; for (int i = 0; i < pathName.length; i++) { compress(new File(pathName[i]), out, basedir); } out.close(); } catch (Exception e) { throw new RuntimeException(e); } } public void compress(String srcPathName) { File file = new File(srcPathName); if (!file.exists()) throw new RuntimeException(srcPathName + "不存在!"); try { FileOutputStream fileOutputStream = new FileOutputStream(fileName); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos); String basedir = ""; compress(file, out, basedir); out.close(); } catch (Exception e) { throw new RuntimeException(e); } } private void compress(File file, ZipOutputStream out, String basedir) { /* 判斷是目錄還是文件 */ if (file.isDirectory()) { this.compressDirectory(file, out, basedir); } else { this.compressFile(file, out, basedir); } } /** * 壓縮目錄 * @param dir * @param out * @param basedir */ private void compressDirectory(File dir, ZipOutputStream out, String basedir) { if (!dir.exists()) return; File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { /* 遞歸 */ compress(files[i], out, basedir + dir.getName() + "/"); } } /** * 壓縮文件 * @param file * @param out * @param basedir */ private void compressFile(File file, ZipOutputStream out, String basedir) { if (!file.exists()) { return; } try { BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file)); String filePath = (basedir + file.getName()) .replaceAll(getOriginalUrl() + "/", ""); log.info("壓縮文件:" + filePath); ZipEntry entry = new ZipEntry(filePath); out.putNextEntry(entry); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } bis.close(); } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) { JarCompressor zc = new JarCompressor("/Users/mac/Documents/other/bw2/test.jar"); zc.compress("/Users/mac/Documents/other/bw2/test/"); } public String getOriginalUrl() { return originalUrl; } public void setOriginalUrl(String originalUrl) { this.originalUrl = originalUrl; } }
上述內(nèi)容就是怎么進(jìn)行壓縮jar,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。