Json压缩工具
一般的json文件拥有很多的空格和注释,虽然读起来比较方便,但是在运行的时候是要占一些内存的。
于是json压缩工具就应运而生了,这个工具是用java做的,原理是:
1:在Eclipse中导出一个可运行的jar文件
2:用python运行这个jar文件,并向这个jar文件的运行程序传一些main方法的参数(一般只传路径就可以了)
- //bat文件的代码如下:
- set assetsAPath="..\javaOutput\assets\uijson" 原json文件路径
- set assetsBPath="..\javaOutput\assets\uijson\\" 压缩后的json文件路径
- set targetJar="JSONMinify.jar"
- cd ./lang
- java -jar lib/JSONMinify.jar
- cd ..
- cd ./tools
- java -jar %targetJar% resVersion=%resVersion% assetsAPath=%assetsAPath% assetsBPath=%assetsBPath%
- pause
//java代码如下
- package com.pack;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import com.zhaohe.download.ResourceDownloader;
- public class JSONMinify {
- static ArrayList<String> fileName;
- public static void main(String[] args) throws Exception {
- initArgs(args);
- String fileData1 = assetsAPath;
- String fileDatas2 = assetsBPath;
- fileName = new ArrayList<String>();
- File folder = new File(fileDatas2);
- List<String> filesPath = getAllFile(fileData1, false);
- System.err.println(getAllFile(fileData1, false).size());
- System.err.println(fileData1);
- for (int i = 0; i < getAllFile(fileData1, false).size(); i++) {
- System.err.println(filesPath.get(i));
- File file = new File(filesPath.get(i));// 需要读取的文件
- File file1 = new File(fileDatas2, fileName.get(i));// 需要写进去的文件
- String fileContext = txt2String(file);// 文件内容
- file.delete();
- if (!file1.exists()) {
- file.createNewFile();
- }
- PrintWriter out1 = new PrintWriter(new BuffewedWriter(new OutputStreamWriter(new FileOutputStream(fileDatas2+fileName.get(i)),"utf-8")));
- out1.write(minify(fileContext));
- out.write(minify(fileContext));
- out.close();
- }
- }
- private static final String manifestFileName = "assetsManifest.txt";
- private static final String remoteVersionFile = "assetsVersion.txt";
- private static final String lcoalVersionFile = "localAssetsVersion.txt";
- private static String version = "1.0.2";
- private static String assetsAPath = "..\\android\\assets\\uiname\\";
- private static String assetsBPath = "..\\android\\assets\\uijson\\";
- private static String rootFolder = ".\\packages";
- private static String cdnUrl = "http://www.mathmt.com";
- private static String serverID = "12";
- private static String resVersion = String.valueOf(ResourceDownloader.RES_TYPE_CURRENT);
- public static void initArgs(String args[]) {
- if (args != null) {
- for (String arg : args) {
- String[] keyValue = arg.split("=");
- final String key = keyValue[0];
- final String value = keyValue[1];
- if ("targetVersion".equals(key)) {
- version = value;
- } else if ("assetsAPath".equals(key)) {
- assetsAPath = value;
- } else if ("assetsBPath".equals(key)) {
- assetsBPath = value;
- } else if ("cdnUrl".equals(key)) {
- cdnUrl = value;
- } else if ("rootFolder".equals(key)) {
- rootFolder = value;
- } else if ("serverID".equals(key)) {
- serverID = value;
- } else if ("resVersion".equals(key)) {
- resVersion = value;
- } else {
- System.out.println("Unknown key:" + key);
- }
- }
- }
- }
- public static boolean deleteDir(String path) {
- File file = new File(path);
- if (!file.exists()) {// 判断是否待删除目录是否存在
- return false;
- }
- String[] content = file.list();// 取得当前目录下所有文件和文件夹
- for (String name : content) {
- File temp = new File(path, name);
- if (temp.isDirectory()) {// 判断是否是目录
- deleteDir(temp.getAbsolutePath());// 递归调用,删除目录里的内容
- temp.delete();// 删除空目录
- } else {
- if (!temp.delete()) {// 直接删除文件
- temp.delete();
- System.err.println("Failed to delete " + name);
- }
- }
- }
- return true;
- }
- /*
- * 读取txt文件的内容
- *
- * @param file 想要读取的文件对象
- *
- * @return 返回文件内容
- */
- public static String txt2String(File file) {
- StringBuilder result = new StringBuilder();
- try {
- BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
- String s = null;
- while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
- result.append(System.lineSeparator() + s);
- }
- br.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result.toString();
- }
- /**
- * 以行为单位读取文件,常用于读面向行的格式化文件
- */
- public static void readFileByLines(String fileName) {
- File file = new File(fileName);
- BufferedReader reader = null;
- try {
- System.out.println("以行为单位读取文件内容,一次读一整行:");
- reader = new BufferedReader(new FileReader(file));
- String tempString = null;
- int line = 1;
- // 一次读入一行,直到读入null为文件结束
- while ((tempString = reader.readLine()) != null) {
- // 显示行号
- System.out.println("line " + line + ": " + tempString);
- line++;
- }
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e1) {
- }
- }
- }
- }
- /**
- * 获取路径下的所有文件/文件夹
- *
- * @param directoryPath
- * 需要遍历的文件夹路径
- * @param isAddDirectory
- * 是否将子文件夹的路径也添加到list集合中
- * @return
- */
- public static List<String> getAllFile(String directoryPath, boolean isAddDirectory) {
- List<String> list = new ArrayList<String>();
- File baseFile = new File(directoryPath);
- if (baseFile.isFile() || !baseFile.exists()) {
- return list;
- }
- File[] files = baseFile.listFiles();
- for (File file : files) {
- if (file.isDirectory()) {
- if (isAddDirectory) {
- list.add(file.getAbsolutePath());
- }
- list.addAll(getAllFile(file.getAbsolutePath(), isAddDirectory));
- } else {
- list.add(file.getAbsolutePath());
- fileName.add(file.getName());
- }
- }
- return list;
- }
- public static String minify(String jsonString) {
- boolean in_string = false;
- boolean in_multiline_comment = false;
- boolean in_singleline_comment = false;
- char string_opener = 'x';
- StringBuilder out = new StringBuilder();
- for (int i = 0; i < jsonString.length(); i++) {
- char c = jsonString.charAt(i);
- String cc = jsonString.substring(i, Math.min(i + 2, jsonString.length()));
- if (in_string) {
- if (c == string_opener) {
- in_string = false;
- out.append(c);
- } else if (c == '\\') {
- out.append(cc);
- ++i;
- } else
- out.append(c);
- } else if (in_singleline_comment) {
- if (c == '\r' || c == '\n')
- in_singleline_comment = false;
- } else if (in_multiline_comment) {
- if (cc.equals("*/")) {
- in_multiline_comment = false;
- ++i;
- }
- } else {
- if (cc.equals("/*")) {
- in_multiline_comment = true;
- ++i;
- } else if (cc.equals("//")) {
- in_singleline_comment = true;
- ++i;
- } else if (c == '"' || c == '\'') {
- in_string = true;
- string_opener = c;
- out.append(c);
- } else if (!Character.isWhitespace(c))
- out.append(c);
- }
- }
- return out.toString();
- }
- }
Json压缩工具的更多相关文章
- seajs中spm压缩工具使用
seajs是个好东西,用起来很方便,但是她的压缩工具spm确不被网友看好,因为使用起来很麻烦,捯饬了一天多,终于勉强能压缩了,这里就简单记录一下. 按照地址:http://www.zhangxinxu ...
- python 开发一款图片压缩工具(四):上传图床
上一篇使用了 pngquant 图片压缩工具进行压缩,并通过 click 命令行工具构建了 picom 包.这篇的主要功能是实现图片上传. 图片上传功能的实现 通过 pngquant 压缩图片后,得到 ...
- Springboot 之 Filter 实现 Gzip 压缩超大 json 对象
简介 在项目中,存在传递超大 json 数据的场景.直接传输超大 json 数据的话,有以下两个弊端 占用网络带宽,而有些云产品就是按照带宽来计费的,间接浪费了钱 传输数据大导致网络传输耗时较长 为了 ...
- 使用TSQL查询和更新 JSON 数据
JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...
- 【疯狂造轮子-iOS】JSON转Model系列之二
[疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...
- 【疯狂造轮子-iOS】JSON转Model系列之一
[疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...
- Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)
背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...
- .NET Core系列 : 2 、project.json 这葫芦里卖的什么药
.NET Core系列 : 1..NET Core 环境搭建和命令行CLI入门 介绍了.NET Core环境,本文介绍.NET Core中最重要的一个配置文件project.json的相关内容.我们可 ...
- 一个粗心的Bug,JSON格式不规范导致AJAX错误
一.事件回放 今天工作时碰到了一个奇怪的问题,这个问题很早很早以前也碰到过,不过没想到过这么久了竟然又栽在这里. 当时正在联调一个项目,由于后端没有提供数据接口,于是我直接本地建立了一个 json ...
随机推荐
- python解决四舍五入问题
小数问题是计算机编程中大部分语言都会遇到的问题,尤其是在内容中涉及到评分.金额计算等等,本人一般在解决需求中固定小数位的数字计算时,都会先将其放大整10的倍数至整数,然后计算.存储,只有在显示的时候再 ...
- 【剑指Offer】只出现一次的字符
题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- cplusplus标准库
http://www.cplusplus.com/reference/ Standard C++ Library reference C Library The elements of the C ...
- 2018-2019-20175334实验二《Java面向对象程序设计》实验报告
2018-2019-20175334实验二<Java面向对象程序设计>实验报告 一.实验内容及步骤 实验二面向对象程序设计-1 参考 http://www.cnblogs.com/roce ...
- Ubuntu 16.04安装Docker-CE
系统环境 * Ubuntu: 16.04 * Docker: 17.09.0-ce 安装步骤 1.安装Docker-CE,具体参考:https://docs.docker.com/engine/ins ...
- mvc中让路由忽略带后缀的路径文件
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/ ...
- ionic2 调用 cordova非本地化native 插件方法
1,在项目中添加你的插件 cordova plugin add puginId|puginName|puginUrl|puginPath 2,查看插件clobbers标记 打开项目目录plug ...
- Git世界历险记
Git-版本管理器 Git ||属于分散型版本管理系统,是为版本管理而而设计的软件.(Linux的创始人Linus Torvalds在2005年开发了Git的原型程序,在此之前人们大多选用Subve ...
- Vue 封装可向左向右查看图片列表的组件
<template> <div class="content-container"> <div class="content-contain ...
- k8s 命令补全
安装:apt-get install bash-completion source <(kubectl completion bash) echo "source <(kubec ...