123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- 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;
- import com.hssx.bms.mapper.BookPageMapper;
- 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;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author 吴涛涛
- * @since 2019-10-16
- */
- @Service
- public class BookPageServiceImpl extends ServiceImpl<BookPageMapper, BookPage> implements BookPageService {
- @Value("${upload.path}")
- private String path;
- @Resource
- private BookCategoryMapper bookCategoryMapper;
- @Resource
- private BookPageMapper bookPageMapper;
- @Override
- public HttpRespMsg addBookPage(BookPage bookPage, MultipartFile file) {
- HttpRespMsg msg = new HttpRespMsg();
- if(file != null){
- //上传之前删除数据
- Integer count = bookPageMapper.delete(new QueryWrapper<BookPage>().eq("book_id", bookPage.getBookId()));
- String fileName = UploadFileToFileNameUtil.uploadFile(file, path);
- 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);
- if(!fileHeader.isDirectory()){
- bookPage.setFile(fileHeader.getFileName());
- Integer pageNum = Integer.parseInt(fileHeader.getFileName().substring(0,fileHeader.getFileName().indexOf(".")));
- bookPage.setPageNum(pageNum);
- bookPageMapper.insert(bookPage);
- zipFile2.extractFile(fileHeader, folderPath);
- }else{
- zipFile2.extractFile(fileHeader, folderPath);
- File destFolder = new File(folderPath);
- File[] files = destFolder.listFiles();
- for (File file : files) {
- bookPage.setFile(fileHeader.getFileName());
- Integer pageNum = Integer.parseInt(file.getName().substring(0,file.getName().indexOf(".")));
- bookPage.setPageNum(pageNum);
- bookPageMapper.insert(bookPage);
- }
- }
- }
- 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) {
- HttpRespMsg msg = new HttpRespMsg();
- List<BookPage> list = bookPageMapper.selectList(new QueryWrapper<BookPage>().eq("book_id", bookPage.getBookId()).orderByAsc("page_num"));
- msg.data = list;
- return msg;
- }
- }
|