자바 압축 패키지 파일 이름 난 장 판 문제 해결

6232 단어 자바
한참 을 하 다가 마침내 인터넷 에서 이 해결 방법 을 찾 았 다.
압축 코드

/**
	 * @param path
	 *                  ,      ,       .
	 * @param basePath
	 *              path   ,    new File(path),    :    zip          ,
	 *               null      ,      .
	 * @param zo
	 *                 
	 * @param isRecursive
	 *                
	 * @param isOutBlankDir
	 *                   ,         true,  baseFile  null.
	 * @throws IOException
	 */
	public static void zip(String path, File basePath, ZipOutputStream zo,
			boolean isRecursive, boolean isOutBlankDir) throws IOException {

		File inFile = new File(path);

		File[] files = new File[0];
		if (inFile.isDirectory()) { //    
			files = inFile.listFiles();
		} else if (inFile.isFile()) { //    
			files = new File[1];
			files[0] = inFile;
		}
		byte[] buf = new byte[1024];
		int len;
		// System.out.println("baseFile: "+baseFile.getPath());
		for (int i = 0; i < files.length; i++) {
			String pathName = "";
			if (basePath != null) {
				if (basePath.isDirectory()) {
					pathName = files[i].getPath().substring(
							basePath.getPath().length() + 1);
				} else {//   
					pathName = files[i].getPath().substring(
							basePath.getParent().length() + 1);
				}
			} else {
				pathName = files[i].getName();
			}
			if (files[i].isDirectory()) {
				if (isOutBlankDir && basePath != null) {
					zo.putNextEntry(new ZipEntry(pathName + "/")); //           
				}
				if (isRecursive) { //   
					zip(files[i].getPath(), basePath, zo, isRecursive,
							isOutBlankDir);
				}
			} else {
				FileInputStream fin = new FileInputStream(files[i]);
				zo.putNextEntry(new ZipEntry(pathName));
				while ((len = fin.read(buf)) > 0) {
					zo.write(buf, 0, len);
				}
				fin.close();
			}
		}
		inFile = null;
		files = null;
	}

/**
	 * 
	 *        ZIP 
	 * 
	 * @param directory
	 *                   
	 * @param zipFileName
	 *            ZIP         
	 * @return
	 */
	public static boolean expZip(String directory, String zipFileName) {
		boolean bool = false;
		try {
			File file = new File(directory);
			boolean validate = zipFileName.toLowerCase().endsWith(".zip");
			if (!file.exists() || !validate || !file.isDirectory()) {
				return false;
			} else {

				directory = directory.replace(File.separatorChar, '/');
				zipFileName = zipFileName.replace(File.separatorChar, '/');
				int x = zipFileName.lastIndexOf("/");
				if (x <= -1) {
					return false;
				} else {
					String zipdir = zipFileName.substring(0, x);
					File zipdirs = new File(zipdir);
					zipdirs.mkdirs();
					zipdirs = null;
				}

				OutputStream os = new FileOutputStream(zipFileName);
				BufferedOutputStream bs = new BufferedOutputStream(os);
				ZipOutputStream zo = new ZipOutputStream(bs);
//[color=red]      [/color] 				
zo.setEncoding(System.getProperty("sun.jnu.encoding"));
				ZipOption.zip(directory, new File(directory), zo, true, true);
				zo.closeEntry();
				zo.close();
				bs.close();
				os.close();

				File exit = new File(zipFileName);
				bool = exit.exists();
				exit = null;
			}
			file = null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return bool;
	}

압축 해제 코드

/**
	 *   ZIP  
	 * 
	 * @param zipFileName
	 *            String       zip  
	 * @param extPlace
	 *            String            
	 */
	public static void extZipFileList(String zipFileName, String extPlace)
			throws Exception {
		//   ZIP        
		File fileZip = new File(zipFileName);
		boolean exists = fileZip.exists();//     /       
		if (exists) {
			//   extPlace  
			File dirs = new File(extPlace);
			dirs.mkdirs();
			dirs = null;
			//     
			ZipFile zipFile = null;
			try {
				//[color=red]      [/color]
				System.setProperty("sun.zip.encoding",System.getProperty("sun.jnu.encoding"));
				zipFile = new ZipFile(zipFileName);
				FileInputStream zin = new FileInputStream(zipFileName);
				ZipInputStream zis = new ZipInputStream(zin);
				//     jdk   ZipEntry
				java.util.zip.ZipEntry zipEntry = zis.getNextEntry();
				while (zipEntry != null) {
					String entryName = zipEntry.getName().replace(
							File.separatorChar, '/');
					String names[] = entryName.split("/");
					int length = names.length;
					String path = extPlace;
					for (int v = 0; v < length; v++) {
						if (v < length - 1) {
							path += names[v] + "/";
							new File(path).mkdir();
						} else { //     
							if (entryName.endsWith("/")) { //    ,      
								new File(extPlace + entryName).mkdir();
							} else {
								FileOutputStream fos = new FileOutputStream(
										extPlace + entryName);
								int len;
								byte[] buffer = new byte[1024];
								while ((len = zis.read(buffer)) > 0) {
									fos.write(buffer, 0, len);
								}
								fos.close();
							}
						}
					}
					zis.closeEntry();
					zipEntry = zis.getNextEntry();
				}
			} catch (Exception e) {
				e.printStackTrace();
				if (zipFile != null)
					zipFile.close();
			}
			if (zipFile != null)
				zipFile.close();
		} else {

		}
		fileZip = null;
	}

좋은 웹페이지 즐겨찾기