您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么進行壓縮jar,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
頁面上傳jar包
后臺解壓jar包
頁面展示所有package
選擇一個package
頁面顯示class和子package
選擇class,進入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; } }
上述內容就是怎么進行壓縮jar,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。