|
@@ -1,6 +1,7 @@
|
|
|
package com.hssx.bms.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.hssx.bms.entity.Book;
|
|
|
import com.hssx.bms.entity.BookCategory;
|
|
|
import com.hssx.bms.entity.BookPage;
|
|
|
import com.hssx.bms.mapper.BookCategoryMapper;
|
|
@@ -9,11 +10,15 @@ import com.hssx.bms.service.BookPageService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.hssx.bms.until.HttpRespMsg;
|
|
|
import com.hssx.bms.until.UploadFileToFileNameUtil;
|
|
|
+import net.lingala.zip4j.core.ZipFile;
|
|
|
+import net.lingala.zip4j.exception.ZipException;
|
|
|
+import net.lingala.zip4j.model.FileHeader;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -36,14 +41,62 @@ public class BookPageServiceImpl extends ServiceImpl<BookPageMapper, BookPage> i
|
|
|
public HttpRespMsg addBookPage(BookPage bookPage, MultipartFile file) {
|
|
|
HttpRespMsg msg = new HttpRespMsg();
|
|
|
if(file != null){
|
|
|
- Integer count = bookPageMapper.selectCount(new QueryWrapper<BookPage>().eq("book_id", bookPage.getBookId()));
|
|
|
+ //上传之前删除数据
|
|
|
+ Integer count = bookPageMapper.delete(new QueryWrapper<BookPage>().eq("book_id", bookPage.getBookId()));
|
|
|
String fileName = UploadFileToFileNameUtil.uploadFile(file, path);
|
|
|
- bookPage.setFile(fileName);
|
|
|
- bookPage.setPageNum(count+1);
|
|
|
- bookPageMapper.insert(bookPage);
|
|
|
+ String filePath = path.substring(0, path.length() - "/upload/".length()) + fileName;
|
|
|
+ try {
|
|
|
+ Unzip4j1(filePath,path+bookPage.getBookId(),bookPage);
|
|
|
+ } catch (ZipException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
return msg;
|
|
|
}
|
|
|
+ // 解压方法2
|
|
|
+ public void Unzip4j1(String zipFile,String folderPath,BookPage bookPage) throws ZipException {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ File folder = new File(folderPath);
|
|
|
+ deleteDir(folder);
|
|
|
+ ZipFile zipFile2 = new ZipFile(zipFile);
|
|
|
+ //设置编码格式
|
|
|
+ zipFile2.setFileNameCharset("GBK");
|
|
|
+ if (!zipFile2.isValidZipFile()) {
|
|
|
+ throw new ZipException("文件不合法或不存在");
|
|
|
+ }
|
|
|
+ //检查是否需要密码
|
|
|
+// checkEncrypted(zipFile2);
|
|
|
+ List<FileHeader> fileHeaderList = zipFile2.getFileHeaders();
|
|
|
+ for (int i = 0; i < fileHeaderList.size(); i++) {
|
|
|
+ FileHeader fileHeader = fileHeaderList.get(i);
|
|
|
+ bookPage.setPageNum(i+1);
|
|
|
+ bookPage.setFile(fileHeader.getFileName());
|
|
|
+ bookPageMapper.insert(bookPage);
|
|
|
+ zipFile2.extractFile(fileHeader, folderPath);
|
|
|
+ }
|
|
|
+ System.out.println("解压成功!");
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
+ System.out.println("耗时:" + (endTime - startTime) + "ms");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean deleteDir(File dir) {
|
|
|
+ if (dir.isDirectory()) {
|
|
|
+ String[] children = dir.list();
|
|
|
+ if (children != null && children.length > 0) {
|
|
|
+ // 递归删除目录中的子目录下
|
|
|
+ for (int i = 0; i < children.length; i++) {
|
|
|
+ boolean success = deleteDir(new File(dir, children[i]));
|
|
|
+ if (!success) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 目录此时为空,可以删除
|
|
|
+ return dir.delete();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public HttpRespMsg getBookPageList(BookPage bookPage) {
|