deason 6 anos atrás
pai
commit
2249e533f1

+ 2 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/Constants.java

@@ -43,5 +43,7 @@ public interface Constants {
     String STRUCT_ZIP_NAME = "试卷结构.zip";
     String OBJECTIVE_EXCEL_NAME = "客观题.xlsx";
     String SUBJECTIVE_EXCEL_NAME = "主观题.xlsx";
+    String PAPER_DIR = "paper";
+    String ANSWER_DIR = "answer";
 
 }

+ 40 - 31
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/utils/FileUtils.java

@@ -87,79 +87,88 @@ public class FileUtils {
     /**
      * 文件压缩
      *
-     * @param target  目录或文件
-     * @param zipFile 压缩后的ZIP文件
+     * @param target  待压缩的目录或文件
+     * @param zipFile 压缩后的文件
      */
-    public static boolean doZip(File target, File zipFile) {
+    public static boolean zip(File target, File zipFile) {
         if (target == null || !target.exists()) {
             log.error("目录或文件不能为空!");
             return false;
         }
+
         if (zipFile == null) {
             log.error("待压缩的文件不能为空!");
             return false;
         }
-        ZipOutputStream zipOutStream = null;
+
+        ZipOutputStream zipStream = null;
         OutputStream outStream = null;
         try {
             if (!zipFile.exists()) {
                 zipFile.createNewFile();
             }
+
             outStream = new FileOutputStream(zipFile);
-            zipOutStream = new ZipOutputStream(outStream, Charset.forName("UTF-8"));
+            zipStream = new ZipOutputStream(outStream, Charset.forName("UTF-8"));
+
             if (target.isDirectory()) {
                 File[] files = target.listFiles();
                 if (files.length == 0) {
                     log.error("文件夹内未找到任何文件!");
                     return false;
                 }
+
                 for (File file : files) {
-                    doZip(zipOutStream, file, null);
+                    doZip(zipStream, file, null);
                 }
             } else {
-                doZip(zipOutStream, target, null);
+                doZip(zipStream, target, null);
             }
+            return true;
         } catch (IOException e) {
             log.error(e.getMessage(), e);
+            return false;
         } finally {
-            if (zipOutStream != null) {
-                IOUtils.closeQuietly(zipOutStream);
-            }
-            if (outStream != null) {
-                IOUtils.closeQuietly(outStream);
+            try {
+                if (zipStream != null) {
+                    zipStream.close();
+                }
+                if (outStream != null) {
+                    outStream.close();
+                }
+            } catch (IOException e) {
+                //ignore
             }
         }
-        return true;
     }
 
-    private static void doZip(ZipOutputStream zipOutStream, File target, String parentDir) throws IOException {
-        //log.info("Zip:" + parentDir);
-        if (parentDir == null) {
-            parentDir = "";
-        }
-        if (!"".equals(parentDir) && !parentDir.endsWith(File.separator)) {
-            parentDir += File.separator;
-        }
-        if (target.isDirectory()) {
-            File[] files = target.listFiles();
+    private static void doZip(ZipOutputStream zipStream, File curFile, String dirName) throws IOException {
+        if (dirName == null) dirName = "";
+        String entryName = dirName + curFile.getName();
+        //log.debug("entryName:" + entryName);
+
+        if (curFile.isDirectory()) {
+            String subDirName = entryName + File.separator;
+            File[] files = curFile.listFiles();
             if (files.length > 0) {
                 for (File file : files) {
-                    doZip(zipOutStream, file, parentDir + target.getName());
+                    doZip(zipStream, file, subDirName);
                 }
             } else {
-                zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
-                zipOutStream.closeEntry();
+                //空文件夹暂不处理
+                //zipStream.putNextEntry(new ZipEntry(subDirName));
+                //zipStream.closeEntry();
             }
         } else {
-            InputStream is = new FileInputStream(target);
-            zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
-            int len = 0;
+            int len;
             byte[] bytes = new byte[1024];
+            InputStream is = new FileInputStream(curFile);
+            zipStream.putNextEntry(new ZipEntry(entryName));
             while ((len = is.read(bytes)) > 0) {
-                zipOutStream.write(bytes, 0, len);
+                zipStream.write(bytes, 0, len);
             }
             IOUtils.closeQuietly(is);
-            zipOutStream.closeEntry();
+            zipStream.closeEntry();
         }
     }
 

+ 34 - 6
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/CoursePaperServiceImpl.java

@@ -32,6 +32,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.*;
 
 import static cn.com.qmth.examcloud.core.print.common.Constants.*;
@@ -268,28 +269,33 @@ public class CoursePaperServiceImpl implements CoursePaperService {
             return null;
         }
 
-        //封装待导出的文件列表
+        //封装待导出的试卷文件列表
         List<ExportFileInfo> exportFiles = new ArrayList<>();
         //封装试卷结构-客观题数据
         List<ObjectiveQuestionStructure> objectiveStructures = new ArrayList<>();
         //封装试卷结构-主观题数据
         List<SubjectiveQuestionStructure> subjectiveStructures = new ArrayList<>();
+
         for (CourseStatistic statistic : statistics) {
             CoursePaper paper = statistic.getCoursePaper();
             if (paper == null) {
                 continue;
             }
+
             //命名规则:试卷名_课程代码_试卷类型_试卷或答案
             final String title = String.format("%s_%s_%s_", paper.getPaperName(), paper.getCourseCode(), statistic.getPaperType());
             ExportFileInfo info = new ExportFileInfo();
+
             if (req.getNeedPaper()) {
                 info.setPaperWord(new Pair<>(title + PAPER_DOC_NAME, paper.getPaperWordUrl()));
                 info.setPaperPdf(new Pair<>(title + PAPER_PDF_NAME, paper.getPaperPdfUrl()));
             }
+
             if (req.getNeedAnswer()) {
                 info.setAnswerWord(new Pair<>(title + ANSWER_DOC_NAME, paper.getAnswerWordUrl()));
                 info.setAnswerPdf(new Pair<>(title + ANSWER_PDF_NAME, paper.getAnswerPdfUrl()));
             }
+
             if (req.getNeedStruct()) {
                 //todo
                 //objectiveStructures.add(...)
@@ -298,13 +304,35 @@ public class CoursePaperServiceImpl implements CoursePaperService {
             exportFiles.add(info);
         }
 
-        //文件根目录
+        //打包所有文件
+        return packageFiles(exportFiles);
+    }
+
+    private File packageFiles(List<ExportFileInfo> exportFiles) {
+        //文件存放目录
         final String rootDir = ExportFileInfo.class.getClassLoader().getResource("").getPath() + "/files";
-        final String tempPath = rootDir + "/" + FileUtils.randomUUID();
-        FileUtils.makeDirs(tempPath);
+        final String targetDir = rootDir + "/" + FileUtils.randomUUID();
+        final String paperDir = targetDir + "/" + PAPER_DIR;
+        final String answerDir = targetDir + "/" + ANSWER_DIR;
+        FileUtils.makeDirs(paperDir);
+        FileUtils.makeDirs(answerDir);
+
+        for (ExportFileInfo info : exportFiles) {
+            //todo upyun
+        }
 
-        //todo
-        return null;
+        //压缩打包所有文件
+        File target = new File(targetDir);
+        File zipFile = new File(targetDir + ".zip");
+        FileUtils.zip(target, zipFile);
+
+        try {
+            //压缩完后移除临时文件
+            org.apache.commons.io.FileUtils.deleteDirectory(target);
+        } catch (IOException e) {
+            log.error(e.getMessage());
+        }
+        return zipFile;
     }
 
 }

+ 15 - 0
examcloud-core-print-starter/src/test/java/cn/com/qmth/examcloud/core/print/test/CoursePaperServiceTest.java

@@ -9,9 +9,12 @@ package cn.com.qmth.examcloud.core.print.test;
 
 import cn.com.qmth.examcloud.core.print.entity.CoursePaper;
 import cn.com.qmth.examcloud.core.print.service.CoursePaperService;
+import cn.com.qmth.examcloud.core.print.service.bean.coursepaper.ExportAllReq;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.io.File;
+
 /**
  * @author: fengdesheng
  * @since: 2018/11/08
@@ -54,4 +57,16 @@ public class CoursePaperServiceTest extends BaseTest {
         coursePaperService.syncCoursePaper(coursePaper);
     }
 
+    @Test
+    public void exportAllCoursePaperTest() throws Exception {
+        ExportAllReq req = new ExportAllReq();
+        req.setOrgId(27L);
+        req.setExamId(21L);
+        req.setNeedPaper(true);
+        req.setNeedAnswer(true);
+        req.setNeedStruct(true);
+        File file = coursePaperService.exportAllCoursePaper(req);
+        System.out.println(file.getPath());
+    }
+
 }