• Java工具封装:Html、Css、Javascript文件内容压缩


    1. Javascript压缩

    1.1 closure-compiler

    Maven: https://mvnrepository.com/artifact/com.google.javascript/closure-compiler

    
    
    <dependency>
        <groupId>com.google.javascriptgroupId>
        <artifactId>closure-compilerartifactId>
        <version>v20220502version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    public class ContentConverUtil {
        private static File tempJavascriptDir = FileUtil.mkdir("temp/generateCode/javascript");
    
        /**
         * Javascript压缩
         *
         * @param javascriptCotnent 待压缩的javascript内容
         * @return
         */
        @SneakyThrows
        public static String javascriptCompressor(String javascriptCotnent) {
            String result = javascriptCotnent;
            if (StrUtil.isNotBlank(javascriptCotnent)) {
                File rowjavascriptCotnentTempFile = FileUtil.createTempFile(tempJavascriptDir);
                File compressorjavascriptCotnentTempFile = FileUtil.file(tempJavascriptDir, StrUtil.format("{}.min.js", IdUtil.getSnowflakeNextIdStr()));
                try {
                    FileUtil.writeUtf8String(javascriptCotnent,rowjavascriptCotnentTempFile);
                    String[] args = new String[]{"--js", FileUtil.getAbsolutePath(rowjavascriptCotnentTempFile), "--js_output_file", FileUtil.getAbsolutePath(compressorjavascriptCotnentTempFile), "--warning_level", "QUIET"};
                    Constructor<CommandLineRunner> constructor = ReflectUtil.getConstructor(CommandLineRunner.class, String[].class);
                    CommandLineRunner commandLineRunner = constructor.newInstance((Object)args);
                    ReflectUtil.invoke(commandLineRunner, "doRun", new Object[0]);
                    result = FileUtil.readString(compressorjavascriptCotnentTempFile, StandardCharsets.UTF_8);
                } finally {
                    FileUtil.del(rowjavascriptCotnentTempFile);
                    FileUtil.del(compressorjavascriptCotnentTempFile);
                }
            }
            return result;
        }
    
    }    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    2. Html压缩

    2.1 htmlcompressor

    Maven: https://mvnrepository.com/artifact/com.googlecode.htmlcompressor/htmlcompressor/1.5.2

    
    <dependency>
        <groupId>com.googlecode.htmlcompressorgroupId>
        <artifactId>htmlcompressorartifactId>
        <version>1.5.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6


    ContentConverUtil.java

    public class ContentConverUtil {
        /**
         * HTML压缩
         * @param htmlCotnent
         * @return
         */
        @SneakyThrows
        public static String htmlCompressor(String htmlCotnent) {
            if(StrUtil.isNotBlank(htmlCotnent)) {
                HtmlCompressor htmlCompressor = new HtmlCompressor();
                return htmlCompressor.compress(htmlCotnent);
            }
            return htmlCotnent;
        }
    }    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3. Css压缩

    3.1 yuicompressor

    Maven: https://mvnrepository.com/artifact/com.yahoo.platform.yui/yuicompressor

    pom.xml

            
            <dependency>
                <groupId>com.yahoo.platform.yuigroupId>
                <artifactId>yuicompressorartifactId>
                <version>2.4.8version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6


    ContentConverUtil.java

    public class ContentConverUtil {
    
        /**
         * CSS压缩
         *
         * @param cssCotnent 待压缩的css内容
         * @return
         */
        @SneakyThrows
        public static String cssCompressor(String cssCotnent) {
            if (StrUtil.isNotBlank(cssCotnent)) {
                BufferedReader reader = null;
                ByteArrayOutputStream resultOutputStream = null;
                Writer writer = null;
                try {
                    reader = IoUtil.getReader(new ByteArrayInputStream(cssCotnent.getBytes()), StandardCharsets.UTF_8);
                    CssCompressor cssCompressor = new CssCompressor(reader);
                    resultOutputStream = new ByteArrayOutputStream();
                    writer = new OutputStreamWriter(resultOutputStream, StandardCharsets.UTF_8);
                    cssCompressor.compress(writer, -1);
                } finally {
                    IoUtil.close(reader);
                    IoUtil.close(writer);  // 必须先关闭流,字符数组才有数据
                }
                cssCotnent = IoUtil.toStr(resultOutputStream, StandardCharsets.UTF_8);
            }
            return cssCotnent;
        }
    
    }    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    Python 利用pandas和matplotlib绘制圆环图
    nuxt添加aos动效
    术语与定义
    直播邀请函 | 第12届亚洲知识产权营商论坛:共建创新价值 开拓崭新领域
    Web开发-session介绍
    进程(fork()详解)
    华为设备配置大型网络WLAN基本业务
    开发者,你对云计算可能有些误解
    1388. 3n 块披萨
    Goby 漏洞发布|Honeywell PM43 loadfile.lp 文件命令执行漏洞(CVE-2023-3710)
  • 原文地址:https://blog.csdn.net/weixin_39651356/article/details/128072720