项目知识盲区五

x33g5p2x  于2021-12-24 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(285)

java压缩文件

  1. package dhy.com.utils;
  2. import dhy.com.exception.CustomException;
  3. import dhy.com.exception.CustomExceptionType;
  4. import java.io.*;
  5. import java.util.List;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8. public class FileUtils
  9. {
  10. //默认的缓冲区大小
  11. private static final int BUFFER_SIZE = 2 * 1024;
  12. //默认的生成的压缩包名字
  13. private static final String DEFAULT_ZIP_NAME="dhy.zip";
  14. //默认是否保留原来的目录结构
  15. private static final Boolean SAVE_ORIGINAL_STRUCTURE=true;
  16. /** * 压缩成ZIP * @param srcDir 要压缩的文件夹的路径 */
  17. public static void toZip(String srcDir) throws FileNotFoundException
  18. {
  19. File file = new File(srcDir);
  20. if(!file.exists())
  21. {
  22. throw new CustomException(CustomExceptionType.USER_INPUT_ERROR,"下载的资源不存在");
  23. }
  24. FileOutputStream fileOutputStream = new FileOutputStream(file.getParent()+File.separator+DEFAULT_ZIP_NAME);
  25. toZip(srcDir,fileOutputStream,SAVE_ORIGINAL_STRUCTURE);
  26. }
  27. /** * 压缩成ZIP * @param srcDir 要压缩的文件夹的路径 * @param zipFileName 生成的压缩文件的名字 */
  28. public static void toZip(String srcDir,String zipFileName) throws FileNotFoundException
  29. {
  30. File file = new File(srcDir);
  31. if(!file.exists())
  32. {
  33. throw new CustomException(CustomExceptionType.USER_INPUT_ERROR,"下载的资源不存在");
  34. }
  35. FileOutputStream fileOutputStream = new FileOutputStream(file.getParent()+File.separator+DEFAULT_ZIP_NAME);
  36. toZip(srcDir,fileOutputStream,SAVE_ORIGINAL_STRUCTURE);
  37. }
  38. /** * 压缩成ZIP * @param srcDir 压缩文件夹路径 * @param KeepDirStructure 是否保留原来的目录结构 */
  39. public static void toZip(String srcDir, boolean KeepDirStructure) throws FileNotFoundException {
  40. File file = new File(srcDir);
  41. if(!file.exists())
  42. {
  43. throw new CustomException(CustomExceptionType.USER_INPUT_ERROR,"下载的资源不存在");
  44. }
  45. FileOutputStream fileOutputStream = new FileOutputStream(file.getParent()+File.separator+DEFAULT_ZIP_NAME);
  46. toZip(srcDir,fileOutputStream,KeepDirStructure);
  47. }
  48. /** * 压缩成ZIP * @param srcDir 压缩文件夹路径 * @param KeepDirStructure 是否保留原来的目录结构 * @param zipFileName 生成的压缩文件的名字 */
  49. public static void toZip(String srcDir,String zipFileName ,boolean KeepDirStructure) throws FileNotFoundException {
  50. File file = new File(srcDir);
  51. if(!file.exists())
  52. {
  53. throw new CustomException(CustomExceptionType.USER_INPUT_ERROR,"下载的资源不存在");
  54. }
  55. FileOutputStream fileOutputStream = new FileOutputStream(file.getParent()+File.separator+DEFAULT_ZIP_NAME);
  56. toZip(srcDir,fileOutputStream,KeepDirStructure);
  57. }
  58. /** * 压缩成ZIP 方法1 * @param srcDir 压缩文件夹路径 * @param out 压缩文件输出流 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * @throws RuntimeException 压缩失败会抛出运行时异常 */
  59. private static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)
  60. throws RuntimeException{
  61. long start = System.currentTimeMillis();
  62. ZipOutputStream zos = null ;
  63. try {
  64. zos = new ZipOutputStream(out);
  65. File sourceFile = new File(srcDir);
  66. //源文件,输出流,压缩文件的路径,是否保持目录结构
  67. compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
  68. long end = System.currentTimeMillis();
  69. System.out.println("压缩完成,耗时:" + (end - start) +" ms");
  70. } catch (Exception e) {
  71. throw new RuntimeException("zip error from ZipUtils",e);
  72. }finally{
  73. if(zos != null){
  74. try {
  75. zos.close();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. }
  81. }
  82. /** * 压缩成ZIP 方法2 * @param srcFiles 需要压缩的文件列表 * @param out 压缩文件输出流 * @throws RuntimeException 压缩失败会抛出运行时异常 */
  83. public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
  84. long start = System.currentTimeMillis();
  85. ZipOutputStream zos = null ;
  86. try {
  87. zos = new ZipOutputStream(out);
  88. for (File srcFile : srcFiles) {
  89. byte[] buf = new byte[BUFFER_SIZE];
  90. zos.putNextEntry(new ZipEntry(srcFile.getName()));
  91. int len;
  92. FileInputStream in = new FileInputStream(srcFile);
  93. while ((len = in.read(buf)) != -1){
  94. zos.write(buf, 0, len);
  95. }
  96. zos.closeEntry();
  97. in.close();
  98. }
  99. long end = System.currentTimeMillis();
  100. System.out.println("压缩完成,耗时:" + (end - start) +" ms");
  101. } catch (Exception e) {
  102. throw new RuntimeException("zip error from ZipUtils",e);
  103. }finally{
  104. if(zos != null){
  105. try {
  106. zos.close();
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. }
  112. }
  113. /** * 递归压缩方法 * @param sourceFile 源文件 * @param zos zip输出流 * @param name 压缩后的名称 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * @throws Exception */
  114. private static void compress(File sourceFile, ZipOutputStream zos, String name,
  115. boolean KeepDirStructure) throws Exception{
  116. byte[] buf = new byte[BUFFER_SIZE];
  117. if(sourceFile.isFile()){
  118. // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
  119. zos.putNextEntry(new ZipEntry(name));
  120. // copy文件到zip输出流中
  121. int len;
  122. FileInputStream in = new FileInputStream(sourceFile);
  123. while ((len = in.read(buf)) != -1){
  124. zos.write(buf, 0, len);
  125. }
  126. // Complete the entry
  127. zos.closeEntry();
  128. in.close();
  129. } else {
  130. File[] listFiles = sourceFile.listFiles();
  131. if(listFiles == null || listFiles.length == 0){
  132. // 需要保留原来的文件结构时,需要对空文件夹进行处理
  133. if(KeepDirStructure){
  134. // 空文件夹的处理
  135. zos.putNextEntry(new ZipEntry(name + "/"));
  136. // 没有文件,不需要文件的copy
  137. zos.closeEntry();
  138. }
  139. }else {
  140. for (File file : listFiles) {
  141. // 判断是否需要保留原来的文件结构
  142. if (KeepDirStructure) {
  143. // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
  144. // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
  145. compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
  146. } else {
  147. compress(file, zos, file.getName(),KeepDirStructure);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }

使用ZipEntry压缩与解压缩

java实现文件打包压缩处理

java文件压缩工具类,打包zip

工具类2:用java进行多文件压缩为一个ZIP包

Java实现将文件或者文件夹压缩成zip

ZipOutputStream、ZipFile、ZipInputStream

Java IO操作——掌握压缩流的使用(ZipOutputStream、ZipFile、ZipInputStream)[java.util包中]

ZipOutputStream使用

CRC32 算法

在远距离数据通信中,为确保高效而无差错地传送数据,必须对数据进行校验即差错控制。循环冗余校验CRC(Cyclic Redundancy Check/Code)是对一个传送数据块进行校验,是一种高效的差错控制方法。

CRC32 算法

Java安全管理器SecurityManager

Java安全管理器SecurityManager

文件下载

SpringBoot实现文件下载

Spring Boot实现文件上传与下载

  1. //下载爬取到的图片
  2. public void downLoad() {
  3. ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
  4. HttpSession session = requestAttributes.getRequest().getSession();
  5. //用户是否开启了爬虫线程----主要判断session,如果为空,抛出异常
  6. if(session==null)
  7. throw new CustomException(CustomExceptionType.USER_INPUT_ERROR,"请先爬取再下载资源");
  8. String sessionID =session.getId();
  9. //判断当前用户的爬虫线程是否关闭
  10. List<JobDetail> userClawerList = getUserClawerList();
  11. //当前用户开启了一个爬虫线程,还未关闭,不能继续开启爬虫线程
  12. if (userClawerList != null && userClawerList.size() == 1) {
  13. throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "请先关闭已经开启的爬虫线程");
  14. }
  15. //TODO:将下载的图片,的文件夹进行打包
  16. String zipPath="";
  17. try {
  18. zipPath = FileUtils.toZip(clawerProperties.getFilePathPrefix() + sessionID + "/");
  19. } catch (FileNotFoundException e) {
  20. e.printStackTrace();
  21. }
  22. HttpServletResponse response = requestAttributes.getResponse();
  23. File file = new File(zipPath);
  24. if (file == null||!file.exists())
  25. throw new CustomException(CustomExceptionType.SYSTEM_ERROR, "下载的文件不存在");
  26. response.setContentType("application/octet-stream");
  27. response.setHeader("content-type", "application/octet-stream");
  28. try {
  29. response.addHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode(file.getName(), "UTF-8"));
  30. } catch (UnsupportedEncodingException e) {
  31. e.printStackTrace();
  32. }
  33. byte[] buffer = new byte[1024];
  34. OutputStream os=null;
  35. try (FileInputStream fis = new FileInputStream(file);
  36. BufferedInputStream bis = new BufferedInputStream(fis))
  37. {
  38. os = response.getOutputStream();
  39. int i = -1;
  40. while ((i = bis.read(buffer)) != -1) {
  41. os.write(buffer, 0, i);
  42. }
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }finally {
  46. try {
  47. os.close();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. //删除文件压缩包
  52. FileUtils.delFile(zipPath);
  53. }
  54. }

no main manifest attribute, in xxxx.jar 项目部署报错

no main manifest attribute, in xxxx.jar 项目部署报错

找不到main方法,需要我们在pom.xml指定

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <executions>
  7. <execution>
  8. <phase>package</phase>
  9. <goals>
  10. <goal>repackage</goal>
  11. </goals>
  12. </execution>
  13. </executions>
  14. <configuration>
  15. <includeSystemScope>true</includeSystemScope>
  16. <mainClass>com.qiruipeng.eureka.EurekaApplication</mainClass>
  17. </configuration>
  18. </plugin>
  19. </plugins>
  20. </build>

相关文章