分析语料库
@param zipFile 压缩的语料库
@param output 保存抽取出的文本的文件路径
@param separator 词之间的分隔符
@param includePhrase 是否抽取短语
@throws IOException
482333
private static void analyzeCorpus(String zipFile, String output, final String separator, final boolean includePhrase) throws IOException{
        File outputFile = new File(output);
        //准备输出目录
        if(!outputFile.getParentFile().exists()){
            outputFile.getParentFile().mkdirs();
        }
        try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile), ExtractText.class.getClassLoader());
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile),"utf-8"));) {
            for(Path path : fs.getRootDirectories()){                
                LOGGER.info("处理目录："+path);
                Files.walkFileTree(path, new SimpleFileVisitor<Path>(){

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        LOGGER.info("处理文件："+file);
                        // 拷贝到本地文件系统
                        Path temp = Paths.get("target/corpus-"+System.currentTimeMillis()+".txt");
                        Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                        extractText(temp, writer, separator, includePhrase);
                        return FileVisitResult.CONTINUE;
                    }
                    
                });
            }
        }
    }
791132
public static void extract(final File zipfile, final File outdir) {
        try {
            ZipInputStream zin = new ZipInputStream(new FileInputStream(zipfile));
            ZipEntry entry;
            String name, dir;
            while ((entry = zin.getNextEntry()) != null) {
                name = entry.getName();
                if (entry.isDirectory()) {
                    mkdirs(outdir, name);
                    continue;
                }

                dir = dirpart(name);
                if (dir != null) {
                    mkdirs(outdir, dir);
                }

                extractFile(zin, outdir, name);
            }
            zin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
966356
public static void zipInto(File target, File... files) {
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)));
            byte[] buffer = new byte[128];
            for (File f : files) {
                ZipEntry entry = new ZipEntry(f.getName());
                InputStream is = new BufferedInputStream(new FileInputStream(f));
                zos.putNextEntry(entry);
                int read = 0;
                while ((read = is.read(buffer)) != -1) {
                    zos.write(buffer, 0, read);
                }
                zos.closeEntry();
                IO.close(is);
            }
        } catch (IOException e) {
            throw E.ioException(e);
        } finally {
            IO.close(zos);
        }
    }
761275
public static List<File> extract(File zipFile, File outputFolder) throws IOException {
    List<File> extracted = new ArrayList<File>();

    byte[] buffer = new byte[2048];

    if (!outputFolder.exists()) {
      outputFolder.mkdir();
    }

    ZipInputStream zipInput = new ZipInputStream(new FileInputStream(zipFile));

    ZipEntry zipEntry = zipInput.getNextEntry();

    while (zipEntry != null) {

      String neFileNameName = zipEntry.getName();
      File newFile = new File(outputFolder + File.separator + neFileNameName);

      newFile.getParentFile().mkdirs();

      if (!zipEntry.isDirectory()) {
        FileOutputStream fos = new FileOutputStream(newFile);

        int size;
        while ((size = zipInput.read(buffer)) > 0) {
          fos.write(buffer, 0, size);
        }

        fos.close();
        extracted.add(newFile);
      }

      zipEntry = zipInput.getNextEntry();
    }

    zipInput.closeEntry();
    zipInput.close();

    return extracted;

  }
1237422
private static void compressDirectory(String relativePath, File srcPath, ZipOutputStream zip) throws IOException
   {
      if (srcPath.isDirectory())
      {
         zip.putNextEntry(new ZipEntry(relativePath + "/" + srcPath.getName() + "/"));
         zip.closeEntry();

         String files[] = srcPath.list();
         for (int i = 0; i < files.length; i++)
         {
            compressDirectory(relativePath + "/" + srcPath.getName(), new File(srcPath, files[i]), zip);
         }
      }
      else
      {
         InputStream in = new FileInputStream(srcPath);
         try
         {
            zip.putNextEntry(new ZipEntry(relativePath + "/" + srcPath.getName()));

            transfer(in, zip);

            zip.closeEntry();
         }
         finally
         {
            if (in != null)
            {
               in.close();
            }
         }
      }
   }
309274
public void addFile(String zipEntry, String filename) throws IOException {
		addFile(zipEntry, new File(filename));
	}
522374
public static void addFileToZipOutputStream(File file, ZipOutputStream zipOs) throws IOException {
        String fileName = file.getName();
        try (FileInputStream fos = new FileInputStream(file)) {
            ZipEntry zipEntry = new ZipEntry(fileName);
            zipEntry.setSize(file.length());
            zipEntry.setTime(System.currentTimeMillis());
            zipOs.putNextEntry(zipEntry);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = fos.read(buf)) > 0) {
                zipOs.write(buf, 0, bytesRead);
            }
            zipOs.closeEntry();
            fos.close();
        }
    }
875430
public static void extractFile(final ZipInputStream in, final File outdir, final String name) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outdir, name)));
        int count = -1;
        while ((count = in.read(buffer)) != -1) {
            out.write(buffer, 0, count);
        }
        out.close();
    }
861258
public static void unzip(InputStream zipFile, String dest) throws IOException {
        byte[] buffer = new byte[1024];

        //create output directory is not exists
        File folder = new File(dest);

        if (folder.exists()) {
            FileUtil.delete(folder);
        }

        folder.mkdir();

        //get the zip file content
        try (ZipInputStream zis = new ZipInputStream(zipFile)) {
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();
            File parent = new File(dest);
            while (ze != null) {
                String fileName = ze.getName();
                if (ze.isDirectory()) {
                    File newFolder = new File(parent, fileName);
                    newFolder.mkdir();
                } else {
                    File newFile = new File(parent, fileName);
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                }
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
        }
    }
780814
private static void compress(File source, ZipArchiveOutputStream zipOut, String zipEntryName) throws IOException {

		String tempZipEntryName = $.notEmpty(zipEntryName) ? zipEntryName : source.getName();

		byte[] buff = new byte[512];
		int readLen = -1;
		if (source.isFile()) {
			// System.out.println("压缩文件：" + source.getAbsolutePath());
			ZipArchiveEntry entry = new ZipArchiveEntry(tempZipEntryName);
			zipOut.putArchiveEntry(entry);
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
			while ((readLen = bis.read(buff)) != -1) {
				zipOut.write(buff, 0, readLen);
			}
			zipOut.closeArchiveEntry();
			IOUtils.closeQuietly(bis);
		} else if (source.isDirectory()) {
			// System.out.println("**** 压缩目录：" + source.getAbsolutePath() +
			ZipArchiveEntry entry = new ZipArchiveEntry(tempZipEntryName.concat(File.separator));
			zipOut.putArchiveEntry(entry);
			zipOut.closeArchiveEntry();
			File[] files = source.listFiles();
			if ($.notEmpty(files)) {
				for (File file : files) {
					compress(file, zipOut, tempZipEntryName + File.separator + file.getName());
				}
			}
		}
	}
600785
private function copyToOutputAndCleanup($output, $zipFileUrl)
    {
        $zipFilePointer = fopen($zipFileUrl, 'r');
        if (!stream_copy_to_stream($zipFilePointer, $output)
            || !fclose($zipFilePointer)
            || !fclose($output)
            || !unlink($zipFileUrl)
        ) {
            throw new \RuntimeException("Failed to copy stream and clean up!");
        }
    }
1168520
public void unpack(File file, File outputDirectory) {
    ZipFile zipFile = null;
    try {
      zipFile = new ZipFile(file);
      Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
      while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        if (!exclude(entry.getName(), excludeFiles)) {
          unpackEntry(zipFile, entry, outputDirectory);
        }
      }
    }
    catch (IOException ex) {
      throw new PackageManagerException("Error reading content package " + file.getAbsolutePath(), ex);
    }
    finally {
      IOUtils.closeQuietly(zipFile);
    }
  }
220390
public static void compress(File dir, File zipFile) throws IOException {

    FileOutputStream fos = new FileOutputStream(zipFile);
    ZipOutputStream zos = new ZipOutputStream(fos);

    recursiveAddZip(dir, zos, dir);

    zos.finish();
    zos.close();

  }
95321
private boolean unZip(String innerDir, String archiveFile) {
        boolean success = true;
        ZipFile zipFile;
        try {
            zipFile = new ZipFile(archiveFile);
            // Get the list of file headers from the zip file before unpacking
            List fileHeaderList = zipFile.getFileHeaders();

            List<PathMatcher> matchers = Arrays.stream(filesExcludes).map(fileExclude ->
                    FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + fileExclude)).collect(Collectors.toList());
            // Loop through the file headers and extract only files that are not matched by fileExcludes patterns
            for (int i = 0; i < fileHeaderList.size(); i++) {
                FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
                String fileName = fileHeader.getFileName();
                if (filesExcludes.length > 0) {
                    Predicate<PathMatcher> matchesExcludes = pathMatcher -> pathMatcher.matches(Paths.get(innerDir, fileName));
                    if (matchers.stream().noneMatch(matchesExcludes)) {
                        zipFile.extractFile(fileHeader, innerDir);
                    }
                } else {
                    zipFile.extractFile(fileHeader, innerDir);
                }
            }
        } catch (Exception e) {
            success = false;
            logger.warn("Error extracting file {}: {}", archiveFile, e.getMessage());
            logger.debug("Error extracting file {}: {}", archiveFile, e.getStackTrace());
        } finally {
            // remove reference to zip file
            zipFile = null;
        }
        return success;
    }
609780
private synchronized static void unzip(InputStream zipFileInputStream, String destinationDir) throws IOException {
        // Create output directory if it doesn't exist
        File dir = new File(destinationDir);
        if (!dir.exists()) dir.mkdirs();

        // Buffer for read and write data to file
        byte[] buffer = new byte[1024];
        ZipInputStream zis = new ZipInputStream(zipFileInputStream);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(destinationDir + File.separator + fileName);

            // Check if it's a Directory entry
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                // Save file to disk
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }

            // Close current entry and move to next entry
            zis.closeEntry();
            ze = zis.getNextEntry();
        }

        // Close last entry
        zis.closeEntry();
        zis.close();
    }
350555
public static ListPhrase from(@NonNull CharSequence separator) {
    checkNotNull("separator", separator);
    return ListPhrase.from(separator, separator, separator);
  }
1109303
private static boolean addFolderContentToZip(File folder, ZipArchiveOutputStream zipOutput, String folderFromRoot){
		FileInputStream fileInput;
		try{
			for(File currFile : folder.listFiles()){
				ZipArchiveEntry entry = null;
				if(currFile.isDirectory()){
					//as defined by ZipArchiveEntry:
					//Assumes the entry represents a directory if and only if the name ends with a forward slash "/".
					entry = new ZipArchiveEntry(folderFromRoot+currFile.getName()+"/");
					zipOutput.putArchiveEntry(entry);
					zipOutput.closeArchiveEntry();
					//now look for the content of this directory
					if(!addFolderContentToZip(currFile,zipOutput,folderFromRoot+currFile.getName()+"/")){
						return false;
					}
				}
				else{
					entry = new ZipArchiveEntry(folderFromRoot+currFile.getName());
					entry.setSize(currFile.length());
					zipOutput.putArchiveEntry(entry);
					
					fileInput = new FileInputStream(currFile);
					IOUtils.copy(fileInput, zipOutput);
					fileInput.close();
					zipOutput.closeArchiveEntry();
				}
			}
		}catch(FileNotFoundException fnfEx){
			fnfEx.printStackTrace();
			return false;
		}catch (IOException ioEx) {
			ioEx.printStackTrace();
			return false;
		}
		return true;
	}