对流中的数据加入到压缩文件<br>

@param zipFile 生成的Zip文件，包括文件名。注意：zipPath不能是srcPath路径下的子文件夹
@param path 流数据在压缩文件中的路径或文件名
@param data 要压缩的数据
@param charset 编码
@return 压缩文件
@throws UtilException IO异常
@since 3.2.2
426383
public static File zip(File zipFile, String path, String data, Charset charset) throws UtilException {
		return zip(zipFile, path, IoUtil.toStream(data, charset), charset);
	}
828666
public static File zip(File zipFile, boolean withSrcDir, File... srcFiles) throws UtilException {
		return zip(zipFile, DEFAULT_CHARSET, withSrcDir, srcFiles);
	}
900308
public static File zip(File zipFile, String[] paths, InputStream[] ins, Charset charset) throws UtilException {
		if (ArrayUtil.isEmpty(paths) || ArrayUtil.isEmpty(ins)) {
			throw new IllegalArgumentException("Paths or ins is empty !");
		}
		if (paths.length != ins.length) {
			throw new IllegalArgumentException("Paths length is not equals to ins length !");
		}

		ZipOutputStream out = null;
		try {
			out = getZipOutputStream(zipFile, charset);
			for (int i = 0; i < paths.length; i++) {
				addFile(ins[i], paths[i], out);
			}
		} finally {
			IoUtil.close(out);
		}
		return zipFile;
	}
403888
static String encodePath(String content, Charset charset) {
        final StringBuilder buf = new StringBuilder();
        final ByteBuffer bb = charset.encode(content);
        while (bb.hasRemaining()) {
            final int b = bb.get() & 0xff;
            if (PATHSAFE.get(b)) {
                buf.append((char) b);
            } else {
                buf.append("%");
                final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
                final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
                buf.append(hex1);
                buf.append(hex2);
            }
        }
        return buf.toString();
    }
1336092
public static Writer buildBufferedAppendWriter(Path path, Charset charset) {
        try {
            if (JMPath.notExists(path))
                JMPathOperation.createFileWithParentDirectories(path);
            return Files.newBufferedWriter(path, charset, StandardOpenOption
                    .APPEND);
        } catch (IOException e) {
            return JMExceptionManager.handleExceptionAndReturnNull(log, e,
                    "buildBufferedAppendWriter", path, charset);
        }
    }
1016197
public String encode(String path, Charset charset) {

		int maxBytesPerChar = 10;
		final StringBuilder rewrittenPath = new StringBuilder(path.length());
		ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
		OutputStreamWriter writer = new OutputStreamWriter(buf, charset);

		int c;
		for (int i = 0; i < path.length(); i++) {
			c = path.charAt(i);
			if (safeCharacters.get(c)) {
				rewrittenPath.append((char) c);
			} else if (encodeSpaceAsPlus && c == CharUtil.SPACE) {
				// 对于空格单独处理
				rewrittenPath.append('+');
			} else {
				// convert to external encoding before hex conversion
				try {
					writer.write((char) c);
					writer.flush();
				} catch (IOException e) {
					buf.reset();
					continue;
				}

				byte[] ba = buf.toByteArray();
				for (int j = 0; j < ba.length; j++) {
					// Converting each byte in the buffer
					byte toEncode = ba[j];
					rewrittenPath.append('%');
					HexUtil.appendHex(rewrittenPath, toEncode, false);
				}
				buf.reset();
			}
		}
		return rewrittenPath.toString();
	}
8697
public static String readString(String path, Charset charset) throws IORuntimeException {
		return readString(file(path), charset);
	}
760499
static ZipFile getZipFile(File src, Charset charset) throws IOException {
    if (charset == null) {
      return new ZipFile(src);
    }

    try {
      Constructor<ZipFile> constructor = ZipFile.class.getConstructor(new Class[] { File.class, Charset.class });
      return (ZipFile) constructor.newInstance(new Object[] { src, charset });
    }
    catch (NoSuchMethodException e) {
      throw new IllegalStateException(MISSING_METHOD_PLEASE_UPGRADE, e);
    }
    catch (InstantiationException e) {
      throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_ZIPFILE + e.getMessage(), e);
    }
    catch (IllegalAccessException e) {
      throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_ZIPFILE + e.getMessage(), e);
    }
    catch (IllegalArgumentException e) {
      throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_ZIPFILE + e.getMessage(), e);
    }
    catch (InvocationTargetException e) {
      throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_ZIPFILE + e.getMessage(), e);
    }
  }
748444
public static BufferedReader getReader(Path path, Charset charset) throws IORuntimeException {
		return IoUtil.getReader(getInputStream(path), charset);
	}
993157
public static Map<String, String[]> loadCorpusWithException(String folderPath, String charsetName) throws IOException
    {
        if (folderPath == null) throw new IllegalArgumentException("参数 folderPath == null");
        File root = new File(folderPath);
        if (!root.exists()) throw new IllegalArgumentException(String.format("目录 %s 不存在", root.getAbsolutePath()));
        if (!root.isDirectory())
            throw new IllegalArgumentException(String.format("目录 %s 不是一个目录", root.getAbsolutePath()));

        Map<String, String[]> dataSet = new TreeMap<String, String[]>();
        File[] folders = root.listFiles();
        if (folders == null) return null;
        for (File folder : folders)
        {
            if (folder.isFile()) continue;
            File[] files = folder.listFiles();
            if (files == null) continue;
            String[] documents = new String[files.length];
            for (int i = 0; i < files.length; i++)
            {
                documents[i] = readTxt(files[i], charsetName);
            }
            dataSet.put(folder.getName(), documents);
        }

        return dataSet;
    }
391664
@SuppressWarnings("unchecked")
	public static File unzip(File zipFile, File outFile, Charset charset) throws UtilException {
		charset = (null == charset) ? DEFAULT_CHARSET : charset;

		ZipFile zipFileObj = null;
		try {
			zipFileObj = new ZipFile(zipFile, charset);
			final Enumeration<ZipEntry> em = (Enumeration<ZipEntry>) zipFileObj.entries();
			ZipEntry zipEntry = null;
			File outItemFile = null;
			while (em.hasMoreElements()) {
				zipEntry = em.nextElement();
				//FileUtil.file会检查slip漏洞，漏洞说明见http://blog.nsfocus.net/zip-slip-2/
				outItemFile = FileUtil.file(outFile, zipEntry.getName());
				if (zipEntry.isDirectory()) {
					outItemFile.mkdirs();
				} else {
					FileUtil.touch(outItemFile);
					copy(zipFileObj, zipEntry, outItemFile);
				}
			}
		} catch (IOException e) {
			throw new UtilException(e);
		} finally {
			IoUtil.close(zipFileObj);
		}
		return outFile;
	}
1083728
public static File zip(File zipFile, String path, InputStream in) throws UtilException {
		return zip(zipFile, path, in, DEFAULT_CHARSET);
	}
397695
@SuppressWarnings("unchecked")
	public static byte[] unzipFileBytes(File zipFile, Charset charset, String name) {
		ZipFile zipFileObj = null;
		try {
			zipFileObj = new ZipFile(zipFile, charset);
			final Enumeration<ZipEntry> em = (Enumeration<ZipEntry>) zipFileObj.entries();
			ZipEntry zipEntry = null;
			while (em.hasMoreElements()) {
				zipEntry = em.nextElement();
				if (zipEntry.isDirectory()) {
					continue;
				} else if (name.equals(zipEntry.getName())) {
					return IoUtil.readBytes(zipFileObj.getInputStream(zipEntry));
				}
			}
		} catch (IOException e) {
			throw new UtilException(e);
		} finally {
			IoUtil.close(zipFileObj);
		}
		return null;
	}
988374
public void writeModel(String path) {
        try (FileOutputStream fso = new FileOutputStream(path)) {
            ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(fso));
            oos.writeUTF(CRFModel.VERSION);
            oos.writeObject(status);
            oos.writeObject(config.getTemplate());
            Map<String, float[]> map = featureTree.toMap();
            MapCount<Integer> mc = new MapCount<>();
            for (float[] v : map.values()) {
                mc.add(v.length);
            }
            for (Entry<Integer, Double> entry : mc.get().entrySet()) {
                int win = entry.getKey();
                oos.writeInt(win);// 宽度
                oos.writeInt(entry.getValue().intValue());// 个数
                for (Entry<String, float[]> e : map.entrySet()) {
                    if (e.getValue().length == win) {
                        oos.writeUTF(e.getKey());
                        float[] value = e.getValue();
                        for (int i = 0; i < win; i++) {
                            oos.writeFloat(value[i]);
                        }
                    }
                }
            }
            oos.writeInt(0);
            oos.writeInt(0);
            oos.flush();
        } catch (FileNotFoundException e) {
            logger.warn("文件没有找到", e);
        } catch (IOException e) {
            logger.warn("IO异常", e);
        }
    }
1068334
public void readOurCorpus(String path, String suffix, String charset) throws IOException {
		List<File> files = MyFiles.getAllFiles(path, suffix);//".txt"
		
		Iterator<File> it = files.iterator();
		while(it.hasNext()){
			BufferedReader bfr =null;
			File file = it.next();
			try {
				FileInputStream in = new FileInputStream(file);
				bfr = new BufferedReader(new UnicodeReader(in,charset));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			FNLPDoc doc = new FNLPDoc();
			doc.name = file.getName();
			
			String line = null;
			while ((line = bfr.readLine()) != null) {
				line = line.trim();
				if (line.matches("^$"))
					continue;
				String[] toks = line.split("\\s+");
				assert(toks.length%5==0);
				int len = toks.length/5;
				
				FNLPSent sent = new FNLPSent(len);
				int base = 0;
				for(int i=0;i<len;i++){
					int idx = Integer.valueOf( toks[base]);
					sent.words[idx] = toks[base+1]; 
					sent.tags[idx] = toks[base+2];
					sent.heads[idx] = Integer.valueOf(toks[base+3]);
					sent.relations[idx] = toks[base+4];
					base += 5;
				}
				
				doc.add(sent);
			}			
			add(doc);
		}
	}
406327
public static String unGzip(byte[] buf, String charset) throws UtilException {
		return StrUtil.str(unGzip(buf), charset);
	}
448368
private static void zip(File file, String srcRootDir, ZipOutputStream out) throws UtilException {
		if (file == null) {
			return;
		}

		final String subPath = FileUtil.subPath(srcRootDir, file); // 获取文件相对于压缩文件夹根目录的子路径
		if (file.isDirectory()) {// 如果是目录，则压缩压缩目录中的文件或子目录
			final File[] files = file.listFiles();
			if (ArrayUtil.isEmpty(files) && StrUtil.isNotEmpty(subPath)) {
				// 加入目录，只有空目录时才加入目录，非空时会在创建文件时自动添加父级目录
				addDir(subPath, out);
			}
			// 压缩目录下的子文件或目录
			for (File childFile : files) {
				zip(childFile, srcRootDir, out);
			}
		} else {// 如果是文件或其它符号，则直接压缩该文件
			addFile(file, subPath, out);
		}
	}