您的位置:首页 > 编程语言 > Java开发

java 从zip包中复制文件

2015-06-21 17:56 513 查看
/**
	 * 从zip包中把AndroidManifest.xml文件拷贝出来
	 * @param file
	 * @param fileName
	 */
	public void copyMainfest(File file) {
		ZipFile zipFile = null;
		ZipInputStream zipInput = null;
		ZipEntry zipEntry = null;
		OutputStream os = null;
		InputStream is = null;
		
		File mainfestFile = new File(file.getParent() + "\\AndroidManifest.xml");
		if(!mainfestFile.exists()){
			try {
				mainfestFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			zipFile = new ZipFile(file);
			zipInput = new ZipInputStream(new FileInputStream(file),Charset.forName("utf-8"));
			os = new FileOutputStream(mainfestFile);
			while((zipEntry = zipInput.getNextEntry()) != null){
				if(zipEntry.getName().equals("AndroidManifest.xml")){
					is = zipFile.getInputStream(zipEntry);
					int len;
					while((len = is.read()) != -1){
						os.write(len);
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				is.close();
				os.close();
				zipInput.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: