提交 0ea2acf8 authored 作者: Matrix's avatar Matrix

更新了FileCreator与FileReader

上级 3a698c10
流水线 #204 已取消 于阶段
...@@ -65,6 +65,13 @@ ...@@ -65,6 +65,13 @@
<version>3.2.0</version> <version>3.2.0</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.yy/android-util -->
<dependency>
<groupId>com.yy</groupId>
<artifactId>android-util</artifactId>
<version>1.0.2</version>
</dependency>
<!--swagger2 enable dependency--> <!--swagger2 enable dependency-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
......
package com.zjty.fp.api.misc.entity;
/**
* PsspCount.
*
* @author Matrix <xhyrzldf@gmail.com>
* @since 2020/7/29 at 12:20 下午
*/
public class PsspCount {
/**
* 代表下一个文件应当用的后缀号
*/
public static int count = -1;
public static final String COUNT_PATH = "files/pssp/count";
public static final String COUNT_ADDRESS = "files/pssp/count/count.txt";
}
package com.zjty.fp.api.misc.utils;
import com.loopj.android.http.Base64;
import javax.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
/**
* DeflaterUtils 压缩字符串
*
* @author Matrix <xhyrzldf@gmail.com>
* @since 2020/7/29 at 9:43 上午
*/
public class DeflaterUtils {
/**
* 压缩
*/
public static String zipString(String unzipString) {
/**
*
* 0 ~ 9 压缩等级 低到高
* public static final int BEST_COMPRESSION = 9; 最佳压缩的压缩级别。
* public static final int BEST_SPEED = 1; 压缩级别最快的压缩。
* public static final int DEFAULT_COMPRESSION = -1; 默认压缩级别。
* public static final int DEFAULT_STRATEGY = 0; 默认压缩策略。
* public static final int DEFLATED = 8; 压缩算法的压缩方法(目前唯一支持的压缩方法)。
* public static final int FILTERED = 1; 压缩策略最适用于大部分数值较小且数据分布随机分布的数据。
* public static final int FULL_FLUSH = 3; 压缩刷新模式,用于清除所有待处理的输出并重置拆卸器。
* public static final int HUFFMAN_ONLY = 2; 仅用于霍夫曼编码的压缩策略。
* public static final int NO_COMPRESSION = 0; 不压缩的压缩级别。
* public static final int NO_FLUSH = 0; 用于实现最佳压缩结果的压缩刷新模式。
* public static final int SYNC_FLUSH = 2; 用于清除所有未决输出的压缩刷新模式; 可能会降低某些压缩算法的压缩率。
*/
//使用指定的压缩级别创建一个新的压缩器。
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
//设置压缩输入数据。
deflater.setInput(unzipString.getBytes());
//当被调用时,表示压缩应该以输入缓冲区的当前内容结束。
deflater.finish();
final byte[] bytes = new byte[256];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
while (!deflater.finished()) {
//压缩输入数据并用压缩数据填充指定的缓冲区。
int length = deflater.deflate(bytes);
outputStream.write(bytes, 0, length);
}
//关闭压缩器并丢弃任何未处理的输入。
deflater.end();
return Base64.encodeToString(outputStream.toByteArray(), Base64.NO_PADDING);
}
/**
* 解压缩
*/
@Nullable
public static String unzipString(String zipString) {
byte[] decode = Base64.decode(zipString, Base64.NO_PADDING);
//创建一个新的解压缩器 https://www.yiibai.com/javazip/javazip_inflater.html
Inflater inflater = new Inflater();
//设置解压缩的输入数据。
inflater.setInput(decode);
final byte[] bytes = new byte[256];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
try {
//finished() 如果已到达压缩数据流的末尾,则返回true。
while (!inflater.finished()) {
//将字节解压缩到指定的缓冲区中。
int length = inflater.inflate(bytes);
outputStream.write(bytes, 0, length);
}
} catch (DataFormatException e) {
e.printStackTrace();
return null;
} finally {
//关闭解压缩器并丢弃任何未处理的输入。
inflater.end();
}
return outputStream.toString();
}
}
package com.zjty.fp.api.misc.utils; package com.zjty.fp.api.misc.utils;
import com.google.common.collect.Lists;
import com.zjty.fp.api.misc.entity.PsspCount;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -13,6 +15,8 @@ import java.text.SimpleDateFormat; ...@@ -13,6 +15,8 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import static com.zjty.fp.api.misc.entity.PsspCount.*;
@Slf4j @Slf4j
@Scope("prototype") @Scope("prototype")
@Component @Component
...@@ -32,9 +36,47 @@ public class FileCreator { ...@@ -32,9 +36,47 @@ public class FileCreator {
return new File(pathname + filename); return new File(pathname + filename);
} }
public File createFileAndZip(String subName, String catalog, String data) {
String zipData = DeflaterUtils.zipString(data);
String pathname = "files/" + subName + "/" + catalog + "/";
String filename = getPsspFileName(subName, catalog);
Path path = Paths.get(pathname + filename);
boolean make = createFilePath(new File(pathname));
if (make) {
try {
Files.write(path, zipData.getBytes());
} catch (IOException e) {
log.info("生成文件时出现异常:" + e);
}
}
//生成完毕后需要更新一下count的值
count += 1;
//写入到count文件中去
Path countPath = Paths.get(COUNT_ADDRESS);
//先删除原有的count文件,再写入现有的
try {
Files.deleteIfExists(countPath);
} catch (IOException e) {
log.warn("删除原count文件失败!原因:{}", e.toString());
}
boolean countMake = createFilePath(new File(COUNT_PATH));
if (countMake) {
try {
Files.write(Paths.get(COUNT_ADDRESS), String.valueOf(count).getBytes());
log.info("[pssp]磁盘count值更新成功:count = {}", count);
} catch (IOException e) {
log.info("[pssp]生成文件时出现异常:" + e);
}
}
return new File(pathname + filename);
}
public File createFileNoTime(String subName, String catalog, String data) { public File createFileNoTime(String subName, String catalog, String data) {
String pathname = "files/" + subName + "/" + catalog + "/"; String pathname = "files/" + subName + "/" + catalog + "/";
String filename = subName + "-" + catalog + ".txt"; String filename = subName + "-" + catalog + ".txt";
Path path = Paths.get(pathname + filename); Path path = Paths.get(pathname + filename);
boolean make = createFilePath(new File(pathname)); boolean make = createFilePath(new File(pathname));
if (make) { if (make) {
...@@ -47,16 +89,16 @@ public class FileCreator { ...@@ -47,16 +89,16 @@ public class FileCreator {
return new File(pathname + filename); return new File(pathname + filename);
} }
public File createFileInUTF(String subName, String catalog,Long index, List<String> list){ public File createFileInUTF(String subName, String catalog, Long index, List<String> list) {
String pathname = "files/" + subName + "/" + catalog + "/"; String pathname = "files/" + subName + "/" + catalog + "/";
String filename = subName + "-" + catalog + "-" +index+".txt"; String filename = subName + "-" + catalog + "-" + index + ".txt";
Path path = Paths.get(pathname + filename); Path path = Paths.get(pathname + filename);
boolean make = createFilePath(new File(pathname)); boolean make = createFilePath(new File(pathname));
if (make) { if (make) {
try { try {
Files.write(path,list); Files.write(path, list);
} catch (IOException e) { } catch (IOException e) {
log.info("生成文件时出现异常:{}" + e); log.info("生成文件时出现异常: " + e);
} }
} }
return new File(pathname + filename); return new File(pathname + filename);
...@@ -69,24 +111,25 @@ public class FileCreator { ...@@ -69,24 +111,25 @@ public class FileCreator {
boolean make = createFilePath(new File(pathname)); boolean make = createFilePath(new File(pathname));
if (make) { if (make) {
try { try {
Files.write(path,list); Files.write(path, list);
} catch (IOException e) { } catch (IOException e) {
log.info("生成文件时出现异常:" + e.getMessage()); log.info("生成文件时出现异常:" + e.getMessage());
} }
} }
return new File(pathname + filename); return new File(pathname + filename);
} }
public File createFileStms(String subName, String catalog, List<String> list){
public File createFileStms(String subName, String catalog, List<String> list) {
String pathname = "files/" + subName + "/" + catalog + "/"; String pathname = "files/" + subName + "/" + catalog + "/";
String filename = getFileNameByDateStms(subName, catalog); String filename = getFileNameByDateStms(subName, catalog);
String fileNameTmp=getFileNameByDateStmsTmp(subName, catalog); String fileNameTmp = getFileNameByDateStmsTmp(subName, catalog);
Path path = Paths.get(pathname + filename); Path path = Paths.get(pathname + filename);
boolean make = createFilePath(new File(pathname)); boolean make = createFilePath(new File(pathname));
if (make) { if (make) {
try { try {
File file= File.createTempFile(filename,".tmp",new File(pathname)); File file = File.createTempFile(filename, ".tmp", new File(pathname));
Files.write(path,list); Files.write(path, list);
new File(pathname + filename).renameTo(new File(pathname+fileNameTmp)); new File(pathname + filename).renameTo(new File(pathname + fileNameTmp));
file.delete(); file.delete();
} catch (IOException e) { } catch (IOException e) {
log.info("生成文件时出现异常:" + e.getMessage()); log.info("生成文件时出现异常:" + e.getMessage());
...@@ -94,17 +137,18 @@ public class FileCreator { ...@@ -94,17 +137,18 @@ public class FileCreator {
} }
return new File(pathname + filename); return new File(pathname + filename);
} }
public File createFileStms(String subName, String catalog, List<String> list,int count){
public File createFileStms(String subName, String catalog, List<String> list, int count) {
String pathname = "files/" + subName + "/" + catalog + "/"; String pathname = "files/" + subName + "/" + catalog + "/";
String filename = getFileNameByDateStms(subName, catalog,count); String filename = getFileNameByDateStms(subName, catalog, count);
String fileNameTmp=getFileNameByDateStmsTmp(subName, catalog,count); String fileNameTmp = getFileNameByDateStmsTmp(subName, catalog, count);
Path path = Paths.get(pathname + filename); Path path = Paths.get(pathname + filename);
boolean make = createFilePath(new File(pathname)); boolean make = createFilePath(new File(pathname));
if (make) { if (make) {
try { try {
File file= File.createTempFile(filename,".tmp",new File(pathname)); File file = File.createTempFile(filename, ".tmp", new File(pathname));
Files.write(path,list); Files.write(path, list);
new File(pathname + filename).renameTo(new File(pathname+fileNameTmp)); new File(pathname + filename).renameTo(new File(pathname + fileNameTmp));
file.delete(); file.delete();
} catch (IOException e) { } catch (IOException e) {
log.info("生成文件时出现异常:" + e.getMessage()); log.info("生成文件时出现异常:" + e.getMessage());
...@@ -112,6 +156,7 @@ public class FileCreator { ...@@ -112,6 +156,7 @@ public class FileCreator {
} }
return new File(pathname + filename); return new File(pathname + filename);
} }
/** /**
* 根据日期生成文件名 * 根据日期生成文件名
* 以天为单位 * 以天为单位
...@@ -122,20 +167,50 @@ public class FileCreator { ...@@ -122,20 +167,50 @@ public class FileCreator {
return subName + "-" + catalog + "-" + sdf.format(new Date()) + ".txt"; return subName + "-" + catalog + "-" + sdf.format(new Date()) + ".txt";
} }
private String getFileNameByDateStms(String subName, String catalog,int count) { private String getFileNameByDateStms(String subName, String catalog, int count) {
return subName + "-" + catalog + count+".tmp"; return subName + "-" + catalog + count + ".tmp";
} }
private String getFileNameByDateStmsTmp(String subName, String catalog,int count) { private String getFileNameByDateStmsTmp(String subName, String catalog, int count) {
return subName + "-" + catalog + count +".txt"; return subName + "-" + catalog + count + ".txt";
} }
private String getFileNameByDateStms(String subName, String catalog) { private String getFileNameByDateStms(String subName, String catalog) {
return subName + "-" + catalog +".tmp"; return subName + "-" + catalog + ".tmp";
} }
private String getFileNameByDateStmsTmp(String subName, String catalog) { private String getFileNameByDateStmsTmp(String subName, String catalog) {
return subName + "-" + catalog +".txt"; return subName + "-" + catalog + ".txt";
}
private String getPsspFileName(String subName, String catalog) {
//count 先从内存里取读 内存里读不到从磁盘读
int count = PsspCount.count;
//如果count为初始值-1,则代表还没有从文件中读取之前的记录
if (count == -1) {
//读取文件获得文件编号后缀值
Path path = Paths.get(COUNT_ADDRESS);
List<String> data = Lists.newArrayList();
try {
data = Files.readAllLines(path);
} catch (IOException e) {
log.info("读取文件时出现异常:" + e);
}
count = data.get(0) == null ? 0 : Integer.parseInt(data.get(0));
}
if (count <= 0) count = 0;
String psspFileName = subName + "-" + catalog + count + ".txt";
log.info("成功生成文件名:{}", psspFileName);
return psspFileName;
} }
/** /**
......
...@@ -2,7 +2,6 @@ package com.zjty.fp.api.vomp.subject.service.updater.impl; ...@@ -2,7 +2,6 @@ package com.zjty.fp.api.vomp.subject.service.updater.impl;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.zjty.fp.api.misc.utils.Downloader; import com.zjty.fp.api.misc.utils.Downloader;
import com.zjty.fp.api.misc.utils.FileReader;
import com.zjty.fp.api.misc.utils.JacksonUtil; import com.zjty.fp.api.misc.utils.JacksonUtil;
import com.zjty.fp.api.vomp.base.enums.VompCatalogs; import com.zjty.fp.api.vomp.base.enums.VompCatalogs;
import com.zjty.fp.api.vomp.base.enums.VompPlatform; import com.zjty.fp.api.vomp.base.enums.VompPlatform;
......
package com.zjty.fp.api.vomp.subject.service.updater.impl; package com.zjty.fp.api.vomp.subject.service.updater.impl;
import com.zjty.fp.api.misc.utils.FileCreator;
import com.zjty.fp.api.misc.utils.JacksonUtil; import com.zjty.fp.api.misc.utils.JacksonUtil;
import com.zjty.fp.api.vomp.subject.repository.remote.*; import com.zjty.fp.api.vomp.subject.repository.remote.*;
import com.zjty.fp.api.vomp.subject.service.updater.VompFetcher; import com.zjty.fp.api.vomp.subject.service.updater.VompFetcher;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论