自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用SpringBoot進行文件壓縮

開發(fā) 前端
我們創(chuàng)建一個實用程序,它可以打包在Java代碼的任何位置,可以在你的微服務(wù)中的任何地方使用。你還可以將其放在共享庫中,作為組織中所有微服務(wù)的實用程序公開。

1 簡介

你知道在Java應(yīng)用程序中優(yōu)化文件服務(wù)器的磁盤空間是非常重要的非功能性要求之一嗎?如果管理得當,可以節(jié)省文件存儲服務(wù)器上60%至70%的成本。因此,對于由Java Spring Boot API生成的數(shù)據(jù)文件進行壓縮顯得尤為非常重要。

Java提供了多個庫來幫助我們壓縮內(nèi)容,同時也提供了本地文件操作來壓縮數(shù)據(jù)文件。在本文中,我們使用本地方法來實現(xiàn)壓縮。

2 使用Java實現(xiàn)文件壓縮和刪除功能

我們創(chuàng)建一個實用程序,它可以打包在Java代碼的任何位置,可以在你的微服務(wù)中的任何地方使用。你還可以將其放在共享庫中,作為組織中所有微服務(wù)的實用程序公開。我們的實用程序?qū)⒕哂袃蓚€功能:

  • 壓縮給定的源文件
  • 在壓縮成功后刪除原始文件

先決條件:

JDK ≥ 1.8 + Maven > 3.X + Spring Boot 2.X + Lombok + Common-IO

我們創(chuàng)建一個對象,該對象將具有基本參數(shù)用于壓縮文件。在下面的代碼中,我們創(chuàng)建了一個名為FileZipper類,它通過Lombok實現(xiàn)Builder和Getter功能。

@Builder(setterPrefix = "set")
@Getter
public class FileZipper {

 @NonNull
 private String sourceFilePath;
 @NonNull
 private String sourceFileName;
 @NonNull
 private String sourceFileExtension;
 @NonNull
 private String zippedFilePath;
 @NonNull
 private String zippedFileName;
 private boolean deleteSourceAfterZipped;
}

現(xiàn)在,我們創(chuàng)建一個名為FileZippingService的Java服務(wù)類,它將根據(jù)用戶輸入壓縮源文件。此服務(wù)類還具有文件刪除功能,可以在文件壓縮成功后刪除源文件。

public class FileZippingService {

 private static final String DOT = ".";
 private static final String ZIP = ".zip";

 private FileZippingService() {
 }

 static Logger logger = LoggerFactory.getLogger(FileZippingService.class);

 /**
  * Method to Zip a file
  * 
  * @param fileZipper
  */
 public static void zipExportFile(FileZipper fileZipper) {

  String sourceFileWithPath = fileZipper.getSourceFilePath().concat(fileZipper.getSourceFileName()).concat(DOT)
    .concat(fileZipper.getSourceFileExtension());
  String zippedFileWithPath = fileZipper.getZippedFilePath().concat(fileZipper.getZippedFileName()).concat(ZIP);
  logger.info("Source File : {} will be zipped into {}", sourceFileWithPath, zippedFileWithPath);
  try {
   File fileToZip = new File(sourceFileWithPath);
   if (fileToZip.exists()) {
    FileOutputStream fos = new FileOutputStream(zippedFileWithPath);
    ZipOutputStream zipOut = new ZipOutputStream(fos);
    FileInputStream fis = new FileInputStream(fileToZip);
    ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
    zipOut.putNextEntry(zipEntry);
    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
     zipOut.write(bytes, 0, length);
    }
    zipOut.close();
    fis.close();
    fos.close();
    logger.info("{} zipped successfully at {}", sourceFileWithPath, zippedFileWithPath);
    if (fileZipper.isDeleteSourceAfterZipped())
     deleteFile(sourceFileWithPath);
   }
  } catch (IOException e) {
   logger.error("Error while Zipping the file", e);
   deleteFile(zippedFileWithPath);
  }

 }

 /**
  * Method to delete a file at given path
  * 
  * @param fileToDelete
  */
 private static void deleteFile(String fileToDelete) {
  File filetoDelete = new File(fileToDelete);
  if (filetoDelete.exists()) {
   if (FileUtils.deleteQuietly(filetoDelete)) {
    logger.info("{} deleted successfully.", fileToDelete);
   } else {
    logger.info("{} deletion unsuccessfull.", fileToDelete);
   }
  } else {
   logger.info("{} was not found for deletion.", fileToDelete);
  }

 }

}

接下來我們通過運行Spring Boot應(yīng)用程序來測試代碼。在下面的Java文件中,你可以看到提供的源文件路徑、名稱和擴展名,從中需要讀取文件;還提供了壓縮文件的文件名以及壓縮后需要存儲的路徑。此外,這里還設(shè)置了一個標志,用于決定是否在壓縮后刪除源文件。

@SpringBootApplication
public class FileZippingApplication {

 public static void main(String[] args) {
  SpringApplication.run(FileZippingApplication.class, args);

  FileZipper fileZipper = FileZipper.builder()
    .setSourceFilePath("C:/Data/")
    .setSourceFileName("UserData")
    .setSourceFileExtension("xlsx")
    .setZippedFileName("ZippedUserData")
    .setZippedFilePath("C:/Data/")
    .setDeleteSourceAfterZipped(true)
    .build();

  FileZippingService.zipExportFile(fileZipper);
 }

}

3 結(jié)果

上述代碼的輸出結(jié)果令人非常驚訝。在我們的一個業(yè)務(wù)應(yīng)用程序中,有大量的CSV文件,大小一般在600MB到1 GB之間,占用消耗大量的磁盤空間,因為我們在一個小時內(nèi)收到了1000多個請求。

但是,在使用上述代碼壓縮后,磁盤空間減少了85%,我們甚至可以通過電子郵件向客戶發(fā)送文件。

責(zé)任編輯:武曉燕 來源: Java學(xué)研大本營
相關(guān)推薦

2016-12-14 09:32:49

FileChanne文件復(fù)制

2022-08-17 12:35:26

Linux sed編輯器

2024-09-29 16:27:46

Python文件管理

2024-11-20 10:00:00

Python文件讀寫

2011-09-01 18:54:29

WifiGoodReader

2018-03-27 13:33:48

百度

2021-08-30 07:57:26

OpenAttack文本對抗攻擊

2010-03-12 19:29:15

python svn腳

2021-09-27 16:39:10

PythonGif壓縮

2023-11-13 18:37:44

2023-06-11 17:00:06

2016-12-14 09:24:42

文件目錄壓縮

2013-07-18 14:16:14

ZipArchive壓iOS開發(fā)

2020-12-31 05:37:05

HiveUDFSQL

2018-01-09 09:00:01

Linux命令文件壓縮

2021-03-31 12:41:24

C語言編程語言

2022-09-08 09:39:03

PythonOCR代碼

2024-06-20 08:09:24

2011-04-08 09:42:19

Access數(shù)據(jù)庫壓縮文件

2017-04-07 09:00:46

UbuntuVim文本選擇
點贊
收藏

51CTO技術(shù)棧公眾號