`
buerkai
  • 浏览: 169548 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

打包解包

阅读更多
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

// 主程序类
public final class CodeFile{
// 此程序的入口点
public static void main(String[] args){
TaskManager taskManager = new TaskManager(args);
taskManager.runTask();
}

// 任务调度类,提供了此程序的主要功能
private static final class TaskManager{
private ParameterManager parameterManager;
private EntityManager entityManager;
private StreamManager streamManager;
private TimerManager timerManager;

public TaskManager(String[] args){
this.parameterManager = new ParameterManager(args);
this.streamManager = new StreamManager();
this.entityManager = new EntityManager(this.streamManager.getContentBytes());
this.timerManager = new TimerManager();
}

// 执行任务
public final void runTask(){
if(this.parameterManager.getCodeModel().equals(ParameterManager.ENCRYPT)){
if(this.parameterManager.getExportModel().equals(ParameterManager.SEPARATE)){
this.runEncryptSeparateTask();
}else{
this.runEncryptIncorporateTask();
}
}else{
if(this.parameterManager.getExportModel().equals(ParameterManager.SEPARATE)){
this.runRecoverSeparateTask();
}else{
this.runRecoverIncorporateTask();
}
}
}

// 执行简单的一对一加密任务
private final void runEncryptSeparateTask(){
String name = this.parameterManager.getFileImportPath().getName();
if(name.equals("")){
name = this.parameterManager.getFileImportPath().getPath().substring(0,1);
}
String exportRoot =
this.parameterManager.getFileExportPath().getPath()
+ ParameterManager.FILE_PATH_DELIMITER + name;
int prefixLength = this.parameterManager.getFileImportPath().getPath().length();
for(int pointer = 0; pointer < this.parameterManager.getFilesAmount(); pointer++){
File importFile = this.parameterManager.getImportFiles()[pointer];
this.streamManager.createContentReader(importFile);
File exportFile =
new File(exportRoot + importFile.getPath().substring(prefixLength));
File filePath = new File(exportFile.getParent());
if(!filePath.exists()){
filePath.mkdirs();
}
this.streamManager.createContentWriter(exportFile);
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
this.entityManager.encryptEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.closeContentWriter();
this.streamManager.closeContentReader();
System.out.println("已处理文件 \"" + importFile + "\"");
System.out.println("结果文件为 \"" + exportFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
}
System.out.println("\n总共处理了" + this.parameterManager.getFilesAmount() + "个文件,处理用时为 "
+ this.timerManager.countTotalTime());
}

// 执行打包式加密任务
private final void runEncryptIncorporateTask(){
String name = this.parameterManager.getFileImportPath().getName();
if(name.equals("")){
name = this.parameterManager.getFileImportPath().getPath().substring(0,1);
}
File contentFile =
new File(this.parameterManager.getFileExportPath()
+ ParameterManager.FILE_PATH_DELIMITER + name);
this.streamManager.createContentWriter(contentFile);
File tempFile = new File(contentFile + ParameterManager.TEMPFILE);
tempFile.deleteOnExit();
this.streamManager.createInfoWriter(tempFile);
int filesAmount = this.parameterManager.getFilesAmount();
for(int pointer = 0; pointer < filesAmount; pointer++){
File importFile = this.parameterManager.getImportFiles()[pointer];
this.streamManager.createContentReader(importFile);
String blockInfo =
importFile.getPath().substring((int)this.parameterManager.getFileImportPath().getPath().length() + 1);
int blockAmount = 0;
int theLastBlockSize = 0;
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
theLastBlockSize = this.streamManager.getContentBytesAmount();
this.entityManager.encryptEntities(EntityTree.root,theLastBlockSize);
this.streamManager.writeContent();
blockAmount++;
}
this.streamManager.closeContentReader();
this.streamManager.writeInfo(blockInfo,blockAmount,theLastBlockSize);
System.out.println("已处理文件 \"" + importFile + "\"");
String time = this.timerManager.countThisTime();
System.out.println("处理用时为 " + time);
}
this.streamManager.closeWriters();
this.streamManager.createContentReader(tempFile);
File infoFile = new File(contentFile + ParameterManager.CODEINFO);
this.streamManager.createContentWriter(infoFile);
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
this.entityManager.encryptEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.closeContentWriter();
this.streamManager.closeContentReader();
System.out.println("\n已处理密码信息文件 \"" + infoFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
System.out.println("\n总共处理了" + filesAmount + "个文件,处理用时为 "
+ this.timerManager.countTotalTime());
System.out.println("\n结果文件集为:");
System.out.println("\"" + contentFile + "\"(密码文件)");
System.out.println("\"" + infoFile + "\"(密码信息文件)");
}

// 执行简单的一对一解密任务
private final void runRecoverSeparateTask(){
String exportRoot = this.parameterManager.getFileExportPath().getPath();
int prefixLength = this.parameterManager.getFileImportPath().getPath().length();
for(int pointer = 0; pointer < this.parameterManager.getFilesAmount(); pointer++){
File importFile = this.parameterManager.getImportFiles()[pointer];
this.streamManager.createContentReader(importFile);
File exportFile =
new File(exportRoot + importFile.getPath().substring(prefixLength));
File filePath = new File(exportFile.getParent());
if(!filePath.exists()){
filePath.mkdirs();
}
this.streamManager.createContentWriter(exportFile);
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
this.entityManager.recoverEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.closeContentWriter();
this.streamManager.closeContentReader();
System.out.println("已处理文件 \"" + importFile + "\"");
System.out.println("结果文件为 \"" + exportFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
}
System.out.println("\n总共处理了" + this.parameterManager.getFilesAmount() + "个文件,处理用时为 "
+ this.timerManager.countTotalTime());
}

// 执行拆包式解密任务
private final void runRecoverIncorporateTask(){
long totalTime = 0l;
int totalFile = 0;
int filesAmount = this.parameterManager.getFilesAmount();
int pointer = 0;
File[] tempFile = new File[filesAmount / 2];
for(; pointer < filesAmount / 2; pointer++){
File infoFile = this.parameterManager.getImportInfoFiles()[pointer];
this.streamManager.createContentReader(infoFile);
tempFile[pointer] = new File(infoFile + ParameterManager.TEMPFILE);
tempFile[pointer].deleteOnExit();
this.streamManager.createContentWriter(tempFile[pointer]);
this.timerManager.setThisTime(System.currentTimeMillis());
while(this.streamManager.readContent()){
this.entityManager.recoverEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.closeContentWriter();
this.streamManager.closeContentReader();
System.out.println("已处理密码信息文件 \"" + infoFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
}
totalTime = this.timerManager.getTotalTime();
this.timerManager.resetTimer();
for(pointer = 0; pointer < filesAmount / 2; pointer++){
File contentFile = this.parameterManager.getImportContentFiles()[pointer];
this.streamManager.createContentReader(contentFile);
this.streamManager.createInfoReader(tempFile[pointer]);
File exportRoot =
new File(this.parameterManager.getFileExportPath()
+ ParameterManager.FILE_PATH_DELIMITER + contentFile.getName());
exportRoot.mkdir();
while(this.streamManager.readInfo()){
String blockInfo = this.streamManager.getBlockInfo();
File resultFile =
new File(exportRoot + ParameterManager.FILE_PATH_DELIMITER + blockInfo);
File filePath = new File(resultFile.getParent());
if(!filePath.exists()){
filePath.mkdirs();
}
this.streamManager.createContentWriter(resultFile);
int blockCount = 0;
int blockAmount = this.streamManager.getBlockAmount();
int theLastBlockSize = this.streamManager.getTheLastBlockSize();
this.timerManager.setThisTime(System.currentTimeMillis());
while(++blockCount < blockAmount){
this.streamManager.readContent();
this.entityManager.recoverEntities(EntityTree.root,this.streamManager.getContentBytesAmount());
this.streamManager.writeContent();
}
this.streamManager.readContent(theLastBlockSize);
this.entityManager.recoverEntities(EntityTree.root,theLastBlockSize);
this.streamManager.writeContent();
this.streamManager.closeContentWriter();
totalFile++;
System.out.println("\n已处理文件 \"" + contentFile + ">>" + blockInfo + "\"");
System.out.println("结果文件为 \"" + resultFile + "\"");
System.out.println("处理用时为 " + this.timerManager.countThisTime());
}
this.streamManager.closeReaders();
System.out.println("\n已处理混合文件 \"" + contentFile + "\"");
System.out.println("结果文件路径为 \"" + exportRoot + "\"");
System.out.println("处理用时为 " + this.timerManager.countTotalTime());
totalTime += this.timerManager.getTotalTime();
this.timerManager.setTotalTime(0l);
}
this.timerManager.setTotalTime(totalTime);
System.out.println("\n\n总共处理了" + filesAmount + "个文件:");
System.out.println("包括" + filesAmount / 2 + "个密码信息文件;" + filesAmount / 2 + "个混合文件(包含"
+ totalFile + "个文件)。");
System.out.println("处理用时为 " + this.timerManager.countTotalTime());
}
}

// 参数管理类,用于解析此程序的输入参数和保存初始化信息
private static final class ParameterManager{
public static final String FILE_PATH_DELIMITER;
public static final String ENCRYPT;
public static final String RECOVER;
public static final String INCORPORATE;
public static final String SEPARATE;
public static final String CODEINFO;
public static final String TEMPFILE;
public static final String HELP_STRING;
private String fileCodeModel;
private String fileExportModel;
private File fileImportPath;
private File fileExportPath;
private File[] importFiles;
private File[] importContentFiles;
private File[] importInfoFiles;
private int filesAmount;
private boolean hasSubDirectories;
static{
FILE_PATH_DELIMITER = System.getProperty("file.separator");
ENCRYPT = "/encrypt";
RECOVER = "/recover";
INCORPORATE = "/incorporate";
SEPARATE = "/separate";
CODEINFO = ".CodeInfo";
TEMPFILE = ".TempFile";
HELP_STRING =
"\n程序名称:CodeFile\n"
+ "程序功能:支持对任意类型的文件进行加密,并支持加密树型目录结构\n"
+ "程序版本:1.0\n"
+ "最后修改日期:2009年4月20日\n"
+ "作者:李允\n\n"
+ "命令行: CodeFile /[encrypt | recover] /[incorporate | separate] ImportFilePath ExportFilePath\n"
+ "参数说明:\n"
+ "encrypt"
+ "\t\t加密文件\n"
+ "recover"
+ "\t\t解密文件\n"
+ "incorporate\t把所有输入文件加密并打包成一个文件,或解密一个或多个被加密并打包的文件\n"
+ "separate"
+ "\t简单的一对一加密或解密文件\n"
+ "ImportFilePath"
+ "\t输入文件路径\n"
+ "ExportFilePath"
+ "\t输出文件路径\n"
+ "注意:ImportFilePath 和 ExportFilePath 必须是目录,且不能为同一目录,ImportFilePath 不能为空目录,ExportFilePath 必须为空目录;"
+ "如果联用 /recover 和 /incorporate 解密文件,ImportFilePath 不能包含子目录,并且其中的密码文件和密码信息文件("
+ CODEINFO + ")是一一对应的,而后者的名称是由前者的名称加 " + CODEINFO + " 组成!";
}

private final void showHelp(){
System.out.println(HELP_STRING);
}

public ParameterManager(String[] args){
this.checkParameters(args);
}

private final void checkParameters(String[] args){
this.checkAmount(args,4);
this.checkModels(args[0],args[1]);
this.checkFilePath(args[2],args[3]);
}

private final void checkAmount(String[] args, int parametersAmount){
if(args.length != parametersAmount){
this.showHelp();
System.exit(0);
}
}

private final void checkModels(String codeModel, String exportModel){
this.fileCodeModel = codeModel;
if(!this.fileCodeModel.equals(ENCRYPT) && !this.fileCodeModel.equals(RECOVER)){
this.showHelp();
System.exit(0);
}
this.fileExportModel = exportModel;
if(!this.fileExportModel.equals(SEPARATE) && !this.fileExportModel.equals(INCORPORATE)){
this.showHelp();
System.exit(0);
}
}

private final void checkFilePath(String importPath, String exportPath){
if(importPath.equalsIgnoreCase(exportPath)){
System.err.println("输入与输出目录不能为同一目录!");
System.exit(0);
}
if(importPath.endsWith(FILE_PATH_DELIMITER)){
importPath = importPath.substring(0,importPath.length() - 1);
}
this.fileImportPath = new File(importPath);
if(!this.fileImportPath.isDirectory()){
System.err.println("\"" + importPath + "\" 不是一个有效的目录!");
System.exit(0);
}
if(exportPath.endsWith(FILE_PATH_DELIMITER)){
exportPath = exportPath.substring(0,exportPath.length() - 1);
}
this.fileExportPath = new File(exportPath);
if(!this.fileExportPath.isDirectory()){
System.err.println("\"" + exportPath + "\" 不是一个有效的目录!");
System.exit(0);
}
if(this.fileExportPath.list().length > 0){
System.err.println("输出目录必须为空目录!");
System.exit(0);
}
final StringBuilder builder = new StringBuilder();
this.recursiveDirectoryTrees(this.fileImportPath,builder);
final String[] filesPathName = builder.toString().split(";");
if(filesPathName[0].length() == 0){
System.err.println("\"" + importPath + "\" 为空目录!");
System.exit(0);
}
this.importFiles = new File[filesPathName.length];
this.filesAmount = this.importFiles.length;
if(this.fileCodeModel.equals(RECOVER) && this.fileExportModel.equals(INCORPORATE)){
if(this.hasSubDirectories || this.filesAmount % 2 != 0){
System.err.println("\"" + this.fileImportPath + "\" 目录中含有子目录或密码文件与密码信息文件("
+ CODEINFO + ")数量不等!");
System.exit(0);
}
}
for(int index = 0; index < this.filesAmount; index++){
this.importFiles[index] = new File(filesPathName[index]);
}
if(this.fileCodeModel.equals(RECOVER) && this.fileExportModel.equals(INCORPORATE)){
int position;
int infoFilesCount = 0;
int contentFilesCount = 0;
for(position = 0; position < this.filesAmount; position++){
if(this.importFiles[position].getName().endsWith(CODEINFO)){
infoFilesCount++;
}else{
contentFilesCount++;
}
}
if(infoFilesCount != contentFilesCount){
System.err.println("\"" + this.fileImportPath + "\" 目录中密码文件与密码信息文件(" + CODEINFO
+ ")数量不等!");
System.exit(0);
}
this.importContentFiles = new File[contentFilesCount];
this.importInfoFiles = new File[infoFilesCount];
contentFilesCount = 0;
infoFilesCount = 0;
for(position = 0; position < this.filesAmount; position++){
if(this.importFiles[position].getName().endsWith(CODEINFO)){
for(int pointer = 0; pointer < this.filesAmount; pointer++){
if(pointer != position
&& this.importFiles[position].getName().startsWith(this.importFiles[pointer].getName())
&& this.importFiles[position].getName().length() == this.importFiles[pointer].getName().length()
+ CODEINFO.length()){
this.importInfoFiles[infoFilesCount++] = this.importFiles[position];
this.importContentFiles[contentFilesCount++] =
this.importFiles[pointer];
}
}
}
}
if(infoFilesCount != this.filesAmount / 2){
System.err.println("\"" + this.fileImportPath + "\" 目录中密码文件与密码信息文件(" + CODEINFO
+ ")名称不匹配!");
System.exit(0);
}
}
}

private final void recursiveDirectoryTrees(File root, final StringBuilder builder){
File[] files = root.listFiles();
if(files != null){
for(int pointer = 0; pointer < files.length; pointer++){
if(files[pointer].isDirectory()){
this.hasSubDirectories = true;
this.recursiveDirectoryTrees(files[pointer],builder);
}else{
builder.append(files[pointer].getPath() + ";");
}
}
}
}

public final String getCodeModel(){
return this.fileCodeModel;
}

public final String getExportModel(){
return this.fileExportModel;
}

public final File getFileImportPath(){
return this.fileImportPath;
}

public final File getFileExportPath(){
return this.fileExportPath;
}

public final File[] getImportFiles(){
return this.importFiles;
}

public final File[] getImportContentFiles(){
return this.importContentFiles;
}

public final File[] getImportInfoFiles(){
return this.importInfoFiles;
}

public final int getFilesAmount(){
return this.filesAmount;
}
}

// 数据处理类,提供了此程序的输入输出数据的方法
private static final class StreamManager{
public static final int BLOCK_SIZE;
public static final byte BLOCK_DELIMITER;
public static final int INFO_SIZE;
private RandomAccessFile contentReader;
private RandomAccessFile contentWriter;
private RandomAccessFile infoReader;
private RandomAccessFile infoWriter;
private byte[] contentBytes;
private int contentBytesAmount;
private byte[] infoBytes;
private int infoBytesAmount;
private int bytesCount;
private String blockInfo;
private int blockAmount;
private int theLastBlockSize;
static{
BLOCK_SIZE = 1048576;
BLOCK_DELIMITER = 0;
INFO_SIZE = 512;
}

public StreamManager(){
this.contentReader = null;
this.contentWriter = null;
this.infoReader = null;
this.infoWriter = null;
this.contentBytes = new byte[BLOCK_SIZE];
this.contentBytesAmount = 0;
this.infoBytes = new byte[INFO_SIZE];
this.infoBytesAmount = 0;
this.bytesCount = 0;
this.blockInfo = "";
this.blockAmount = 0;
this.theLastBlockSize = 0;
}

public final void createContentReader(File file){
try{
this.contentReader = new RandomAccessFile(file,"r");
}catch(IOException e){
e.printStackTrace();
}
}

public final void createInfoReader(File file){
try{
this.infoReader = new RandomAccessFile(file,"r");
}catch(IOException e){
e.printStackTrace();
}
}

public final void createContentWriter(File file){
try{
file.createNewFile();
this.contentWriter = new RandomAccessFile(file,"rw");
}catch(IOException e){
e.printStackTrace();
}
}

public final void createInfoWriter(File file){
try{
file.createNewFile();
this.infoWriter = new RandomAccessFile(file,"rw");
}catch(IOException e){
e.printStackTrace();
}
}

public final boolean readContent(){
try{
this.contentBytesAmount = this.contentReader.read(this.contentBytes);
if(this.contentBytesAmount < 0){
return false;
}
}catch(IOException e){
e.printStackTrace();
}
return true;
}

public final boolean readContent(int bytesAmount){
try{
this.contentBytesAmount = this.contentReader.read(this.contentBytes,0,bytesAmount);
if(this.contentBytesAmount < 0){
return false;
}
}catch(IOException e){
e.printStackTrace();
}
return true;
}

public final boolean readInfo(){
try{
this.infoBytesAmount = this.infoReader.read(this.infoBytes);
if(this.infoBytesAmount < 0){
return false;
}
this.infoBytesToBlockInfo();
}catch(IOException e){
e.printStackTrace();
}
return true;
}

private final void infoBytesToBlockInfo(){
StringBuilder builder = new StringBuilder();
int position = 0;
for(this.bytesCount = 0;; this.bytesCount++){
if(this.infoBytes[this.bytesCount] == BLOCK_DELIMITER){
builder.append(new String(this.infoBytes,position,this.bytesCount - position));
if(this.infoBytes[this.bytesCount + 1] == BLOCK_DELIMITER){
this.blockInfo = builder.toString();
break;
}else{
builder.append(ParameterManager.FILE_PATH_DELIMITER);
position = this.bytesCount + 1;
}
}
}
this.bytesCount += 2;
position = this.bytesCount;
for(; this.infoBytes[this.bytesCount] != BLOCK_DELIMITER; this.bytesCount++)
;
this.blockAmount =
Integer.parseInt(new String(this.infoBytes,position,this.bytesCount - position));
this.bytesCount += 2;
position = this.bytesCount;
for(; this.infoBytes[this.bytesCount] != BLOCK_DELIMITER; this.bytesCount++)
;
this.theLastBlockSize =
Integer.parseInt(new String(this.infoBytes,position,this.bytesCount - position));
}

public final void writeContent(){
try{
this.contentWriter.write(this.contentBytes,0,this.contentBytesAmount);
}catch(IOException e){
e.printStackTrace();
}
}

public final void writeInfo(String blockInfo, int blockAmount, int theLastBlockSize){
try{
this.blockInfoToInfoBytes(blockInfo,blockAmount,theLastBlockSize);
this.infoWriter.write(this.infoBytes);
}catch(IOException e){
e.printStackTrace();
}
}

private final void blockInfoToInfoBytes(String blockInfo, int blockAmount, int theLastBlockSize){
int pointer = 0;
byte[] bytes = blockInfo.getBytes();
byte delimiter = ParameterManager.FILE_PATH_DELIMITER.getBytes()[0];
for(this.bytesCount = 0; pointer < bytes.length; pointer++){
this.infoBytes[this.bytesCount++] = (bytes[pointer] == delimiter?0:bytes[pointer]);
}
for(pointer = 0; pointer < 2; pointer++){
this.infoBytes[this.bytesCount++] = BLOCK_DELIMITER;
}
bytes = Integer.toString(blockAmount).getBytes();
for(pointer = 0; pointer < bytes.length; pointer++){
this.infoBytes[this.bytesCount++] = bytes[pointer];
}
for(pointer = 0; pointer < 2; pointer++){
this.infoBytes[this.bytesCount++] = BLOCK_DELIMITER;
}
bytes = Integer.toString(theLastBlockSize).getBytes();
for(pointer = 0; pointer < bytes.length; pointer++){
this.infoBytes[this.bytesCount++] = bytes[pointer];
}
for(; this.bytesCount < INFO_SIZE; this.bytesCount++){
this.infoBytes[this.bytesCount] = BLOCK_DELIMITER;
}
}

public final void closeReadersAndWriters(){
this.closeWriters();
this.closeReaders();
}

public final void closeReaders(){
this.closeContentReader();
this.closeInfoReader();
}

public final void closeContentReader(){
try{
if(this.contentReader != null){
this.contentReader.close();
this.contentReader = null;
}
}catch(IOException e){
e.printStackTrace();
}
}

public final void closeInfoReader(){
try{
if(this.infoReader != null){
this.infoReader.close();
this.infoReader = null;
}
}catch(IOException e){
e.printStackTrace();
}
}

public final void closeWriters(){
this.closeContentWriter();
this.closeInfoWriter();
}

public final void closeContentWriter(){
try{
if(this.contentWriter != null){
this.contentWriter.close();
this.contentWriter = null;
}
}catch(IOException e){
e.printStackTrace();
}
}

public final void closeInfoWriter(){
try{
if(this.infoWriter != null){
this.infoWriter.close();
this.infoWriter = null;
}
}catch(IOException e){
e.printStackTrace();
}
}

public final byte[] getContentBytes(){
return this.contentBytes;
}

public final int getContentBytesAmount(){
return this.contentBytesAmount;
}

public final String getBlockInfo(){
return this.blockInfo;
}

public final int getBlockAmount(){
return this.blockAmount;
}

public final int getTheLastBlockSize(){
return this.theLastBlockSize;
}
}

// 时间管理类,负责统计处理的每个文件的耗时,精确到毫秒
private static final class TimerManager{
public static final String HOUR;
public static final String MINUTE;
public static final String SECOND;
public static final String MILLISECOND;
private long thisTime;
private long totalTime;
private long hour;
private long minute;
private long second;
private long millisecond;
private String timerReport;
static{
HOUR = "小时";
MINUTE = "分";
SECOND = "秒";
MILLISECOND = "毫秒";
}

public TimerManager(){
this.thisTime = 0l;
this.totalTime = 0l;
this.hour = 0l;
this.minute = 0l;
this.second = 0l;
this.millisecond = 0l;
this.timerReport = "";
}

public final String countThisTime(){
this.thisTime = System.currentTimeMillis() - this.thisTime;
this.totalTime += this.thisTime;
this.countTime(this.thisTime);
return this.timerReport;
}

public final String countTotalTime(){
this.countTime(this.totalTime);
return this.timerReport;
}

private final void countTime(long time){
this.resetTimer();
if(time >= 3600000l){
this.hour = time / 3600000l;
this.minute = (time - this.hour * 3600000l) / 60000l;
this.second = (time - this.hour * 3600000l - this.minute * 60000l) / 1000l;
this.millisecond =
time - this.hour * 3600000l - this.minute * 60000l - this.second * 1000l;
}else if(time >= 60000l){
this.minute = time / 60000l;
this.second = (time - this.minute * 60000l) / 1000l;
this.millisecond = time - this.minute * 60000l - this.second * 1000l;
}else if(time >= 1000l){
this.second = time / 1000l;
this.millisecond = time - this.second * 1000l;
}else{
this.millisecond = time;
}
this.timerReport =
this.hour + HOUR + this.minute + MINUTE + this.second + SECOND + this.millisecond
+ MILLISECOND;
}

private final void resetTimer(){
this.hour = 0l;
this.minute = 0l;
this.second = 0l;
this.millisecond = 0l;
}

public final long getThisTime(){
return this.thisTime;
}

public final void setThisTime(long thisTime){
this.thisTime = thisTime;
}

public final long getTotalTime(){
return this.totalTime;
}

public final void setTotalTime(long totalTime){
this.totalTime = totalTime;
}
}

// 实体管理类,给出了所有基于二叉实体树的方法
private static final class EntityManager{
private final EntityTree[] entities;
private int entitiesCount;
private final byte[] bytes;

public EntityManager(byte[] contentBytes){
this.bytes = contentBytes;
this.entities = new EntityTree[this.bytes.length];
this.entities[0] = EntityTree.root;
for(int i = 1; i < this.bytes.length; i++){
this.entities[i] = new EntityTree();
}
}

// 加密实体
public final void encryptEntities(EntityTree treeRoot, int entitiesAmount){
this.createEntityTree(treeRoot,entitiesAmount);
this.recursiveEncrypt(treeRoot);
}

// 解密实体
public final void recoverEntities(EntityTree treeRoot, int entitiesAmount){
this.createEntityTree(treeRoot,entitiesAmount);
this.recursiveRecover(treeRoot);
this.entitiesContentToBytes(EntityTree.root);
}

// 创建实体树
private final void createEntityTree(EntityTree treeRoot, int entitiesAmount){
this.entitiesCount = 0;
if(entitiesAmount == 1){
treeRoot.reset(this.bytes[0]);
return;
}
treeRoot.setContent(this.bytes[0]);
this.setLeftEntity();
if(entitiesAmount > 2){
this.setRightEntity();
}
for(this.entitiesCount = 1; 2 * this.entitiesCount + 2 < entitiesAmount; this.entitiesCount++){
this.setLeftEntity();
this.setRightEntity();
}
if(2 * this.entitiesCount + 1 < entitiesAmount){
this.setLeftEntity();
}
for(this.entitiesCount = 0; this.entitiesCount < entitiesAmount - 1; this.entitiesCount++){
this.entities[this.entitiesCount].setLink(this.entities[this.entitiesCount + 1]);
}
this.entitiesCount = 0;
}

// 初始化左孩子实体
private final void setLeftEntity(){
this.entities[this.entitiesCount].setLeft(this.entities[2 * this.entitiesCount + 1]);
this.entities[2 * this.entitiesCount + 1].reset(this.bytes[2 * this.entitiesCount + 1]);
}

// 初始化右孩子实体
private final void setRightEntity(){
this.entities[this.entitiesCount].setRight(this.entities[2 * this.entitiesCount + 2]);
this.entities[2 * this.entitiesCount + 2].reset(this.bytes[2 * this.entitiesCount + 2]);
}

// 递归加密
private final void recursiveEncrypt(EntityTree entity){
if(entity != null){
this.recursiveEncrypt(entity.getLeft());
this.bytes[this.entitiesCount++] = entity.getContent();
this.recursiveEncrypt(entity.getRight());
}
}

// 递归解密
private final void recursiveRecover(EntityTree entity){
if(entity != null){
this.recursiveRecover(entity.getLeft());
entity.setContent(this.bytes[this.entitiesCount++]);
this.recursiveRecover(entity.getRight());
}
}

// 导出实体内容到字节数组
private final void entitiesContentToBytes(EntityTree treeRoot){
EntityTree entity = treeRoot;
for(this.entitiesCount = 0; entity != null; entity = entity.getLink()){
this.bytes[this.entitiesCount++] = entity.getContent();
}
}

public final byte[] getBytes(){
return this.bytes;
}
}

// 二叉实体树类,此程序中用来加密解密文件的数据结构
private static final class EntityTree{
// 树根节点
public static final EntityTree root;
// 左孩子节点
private EntityTree left;
// 右孩子节点
private EntityTree right;
// 按照层序遍历方式的当前节点的下一个节点
private EntityTree link;
// 树节点的数据内容
private byte content;
static{
root = new EntityTree();
}

public final void reset(byte content){
this.left = null;
this.right = null;
this.link = null;
this.content = content;
}

public final EntityTree getLeft(){
return this.left;
}

public final void setLeft(EntityTree left){
this.left = left;
}

public final EntityTree getRight(){
return this.right;
}

public final void setRight(EntityTree right){
this.right = right;
}

public final EntityTree getLink(){
return link;
}

public final void setLink(EntityTree link){
this.link = link;
}

public final byte getContent(){
return this.content;
}

public final void setContent(byte content){
this.content = content;
}
}
}
分享到:
评论

相关推荐

    WDetc打包解包工具.zip

    "WDetc打包解包工具.zip" 是一个专用于处理etc文件的实用程序,适用于版本1.60。在IT行业中,etc(等等)通常代表/etc目录,这是一个在Linux和类Unix操作系统中的核心配置目录,存储系统级别的配置文件。这个压缩包...

    彩虹岛SPF打包解包源码

    《彩虹岛SPF打包解包源码》是一个专注于处理彩虹岛游戏中的SPF文件的工具。SPF全称为"Server Package File",是游戏开发者为了方便数据管理与传输,将游戏资源进行压缩打包的一种文件格式。这个源码库提供了解包和...

    Rdb打包解包工具4.0精简版 .zip

    Rdb打包解包工具是一款功能强大的打包结包工具。它可以将一个指定的包文件(类似压缩包那样包含了很多文件或文件夹在里面的文件)中的内容释放到新文件夹中,也可以逆向将一个文件夹中的内容重新打包为一个单独的...

    rk29打包解包工具ultra2.2\RK2918解包打包教程.doc

    rk29打包解包工具ultra2.2\RK2918解包打包教程.doc VCDS ESCWRE

    rez打包解包.zip

    标题中的"rez打包解包.zip"表明这是一个与rez工具相关的压缩包,用于处理游戏资源,特别是针对《穿越火线》(CrossFire, 简称CF)的图标修改。rez工具是游戏模组制作中常用的一种工具,它可以用来打包和解包游戏资源,...

    PCT打包解包工具.exe

    PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT打包解包工具PCT...

    易语言模块数据打包解包.rar

    《易语言模块数据打包解包详解》 在编程领域,数据打包与解包是一项常见的操作,尤其是在使用易语言进行开发时。易语言以其简洁的语法规则和强大的功能深受程序员喜爱,而模块数据的打包与解包是其提高程序效率、...

    apk打包解包工具

    "apk打包解包工具"是一类用于处理APK文件的实用程序,它们可以帮助开发者进行APK的构建、拆解、修改和重新签名等操作。下面将详细阐述相关知识点: 1. **APK打包**:APK打包过程是将Android应用的所有资源(如Java...

    RDB打包解包工具2.3最新版

    1、首先你要准备photoshop软件 和RDB打包解包工具(下载见附件)然后大家在QQ安装目录下找到Res.rdb这个文件 (位于Program Files\Tencent\QQ2010\Resource.1.55.1869文件夹下) 2、之后用RDB打包解包工具进行解包...

    RDB格式文件打包解包工具

    本文将详细探讨RDB打包解包工具及其在实际操作中的应用。 RDB格式文件的打包和解包是Redis数据管理的重要环节。当需要备份Redis的数据时,RDB打包工具能够将RDB文件进行压缩处理,减小存储空间占用,并便于传输。...

    RDB打包解包工具.rar

    "RDB打包解包工具.rar" 提供了一个简洁而有效的解决方案,帮助用户处理数据库(可能指的是关系型数据库,RDB)相关的文件。下面将详细讨论这个工具以及它可能涉及的相关知识点。 首先,"RDB打包"通常是指将数据库的...

    RDB打包解包工具 QQ解包

    本文将重点讨论“RDB打包解包工具”及其在QQ解包中的应用。 “RDB打包解包工具”是一种专门用于处理RDB文件的软件工具,它的主要功能是对RDB数据库进行压缩和解压缩操作。在软件开发和数据分析过程中,打包和解包...

    APK打包解包工具

    APK打包解包工具,这个是手机版本,完全可以完美打包以安装apk

    QQRDB打包解包工具

    QQ RDB打包解包工具是一种专门用于处理QQ应用程序中RDB(Resource Database)文件的工具。RDB文件是腾讯QQ为了存储和管理其应用程序内的资源数据而采用的一种专有格式,通常包含各种配置、图像、音频等资源。下面将...

    打包解包注意位移归零

    这两种类型在打包解包时都需要特别处理,尤其是在位移计算上。 位移归零通常发生在两个场景:一是数组或字符串的初始化,二是数据序列化与反序列化。在C++中,如果我们要将一个`string`对象打包成二进制流,我们...

    RDB打包解包工具

    "RDB打包解包工具"是针对腾讯软件中RDB(Resource Database)文件设计的一款高效工具,主要用于解析腾讯软件内部的图片资源。RDB文件是腾讯为优化其产品性能和资源管理而采用的一种定制化存储格式,它将多个图片资源...

    打包解包(封装数据层,数据抽象,接口抽象)

    下面将详细解释这些概念,并结合"打包解包"、"封装迭代器"、"抽象数据流"以及"接口抽象"来阐述其应用。 首先,**封装**是面向对象编程的基础,它指的是将数据和操作数据的方法绑定在一起,作为一个独立的单元。在这...

    apk打包解包签名工具

    在Android应用开发领域,APK打包解包签名工具有着至关重要的作用,它们帮助开发者进行应用的调试、修改和发布。本篇文章将详细介绍其中的核心工具——`apktool`,以及与之相关的签名过程。 首先,`apktool`是一款...

    Rk29打包解包工具

    Rrk29 android固件打包解包程序,文件内含说明,

    通用文件打包解包助手.exe

    《通用文件打包解包助手详解》 在信息技术领域,数据的打包与解包是一项常见的操作,主要用于文件的传输、存储和备份。通用文件打包解包助手是一款专为此目的设计的实用工具,它可以帮助用户轻松地处理RDB文件的...

Global site tag (gtag.js) - Google Analytics