当前位置: 首页 > 工具软件 > JSON.hpack > 使用案例 >

java json 压缩_Json压缩工具(示例代码)

彭霄
2023-12-01

一般的json文件拥有很多的空格和注释,虽然读起来比较方便,但是在运行的时候是要占一些内存的。

于是json压缩工具就应运而生了,这个工具是用java做的,原理是:

1:在Eclipse中导出一个可运行的jar文件

2:用python运行这个jar文件,并向这个jar文件的运行程序传一些main方法的参数(一般只传路径就可以了)

//bat文件的代码如下:

set assetsAPath="..javaOutputassetsuijson" 原json文件路径

set assetsBPath="..javaOutputassetsuijson\"  压缩后的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 fileName;

public static void main(String[] args) throws Exception {

initArgs(args);

String fileData1 = assetsAPath;

String fileDatas2 = assetsBPath;

fileName = new ArrayList();

File folder = new File(fileDatas2);

List 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();

}

BufferedWriter out = new BufferedWriter(new FileWriter(fileDatas2 + fileName.get(i)));

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 getAllFile(String directoryPath, boolean isAddDirectory) {

List list = new ArrayList();

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 == ‘‘ || c == ‘

‘)

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();

}

}

 类似资料: