⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 archive.java.svn-base

📁 java下操作rar文件,创建,压缩/解压缩等等.
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
						throw new RarException(e);
					}
				}
				
			}else{
				throw new RarException(RarExceptionType.headerNotInArchive);
			}
		}else{
			throw new RarException(RarExceptionType.wrongHeaderType);
		}
	}
	
	private void doExtractFile(FileHeader hd, OutputStream os) throws RarException
	{
		
		ReadOnlyAccessInputStream is = new ReadOnlyAccessInputStream(	
					file,
					hd.getPositionInFile()+hd.getHeaderSize(),
					hd.getPositionInFile()+hd.getHeaderSize()+hd.getFullPackSize()
					);
		
		
		dataIO.init(is, os);
		dataIO.setSubHeader(hd);
		dataIO.setPackedSizeToRead(hd.getFullPackSize());
		dataIO.setCurUnpRead(0);
		dataIO.setCurPackWrite(0);
		dataIO.setUnpFileCRC(this.isOldFormat()?0:0xffFFffFF);
		dataIO.setPackedCRC(0xFFffFFff);
		if(unpack==null){
			unpack = new Unpack(dataIO);
			
		}
		if(!hd.isSolid()){
			unpack.init(null);
		}
		unpack.setDestSize(hd.getFullUnpackSize());
		try {
			unpack.doUnpack(hd.getUnpVersion(), hd.isSolid());
			long hdfilecrc = hd.getFileCRC();
			long unpfilecrc = dataIO.getUnpFileCRC();
			logger.info("CRC "+(~dataIO.getUnpFileCRC()!=hd.getFileCRC()?"Failed\n":"OK\n")+"stored crc: "+Long.toHexString(hdfilecrc)+" :"+hdfilecrc+" current crc: \t"+Long.toHexString(~(unpfilecrc))+" : "+~unpfilecrc);
			if(~dataIO.getUnpFileCRC()!=hd.getFileCRC()){
				throw new RarException(RarExceptionType.crcError);
			}
		} catch (Exception e) {
			if(e instanceof RarException){
				throw new RarException((RarException)e);
			}else{
				throw new RarException(e);
			}
		}
		
		
	}

	
	/**
	 * @return returns the main header of this archive
	 */
	public MainHeader getMainHeader()
	{
		return mainHeader;
	}
	
	/**
	 * not yet supported
	 * @return whether the archive is old format
	 */
	public boolean isOldFormat()
	{
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
				
		try {
			File file = null;
			if(args.length>0){
				logger.info("Trying to read archive: "+args[0]);
				file = new File(args[0]);
			}else{
				StringBuffer str = new StringBuffer();
				//bin files
				//str.append("C:\\winrar-test\\bin-x86\\");
				//str.append("server.rar");												// >ok<
				//str.append("asdf.rar");												// >ok<
				//str.append("visio.rar");												// >ok<
				
				//bin 64bit
				//str.append("C:\\winrar-test\\bin-x86_64\\");
				//str.append("server.rar");												// >ok<
				//str.append("asdf.rar");												// >ok<
				//str.append("visio.rar");												// >ok<
				
				
				//ppmd	
				//str.append("/home/edmund/Desktop/unrar/");
				//str.append("C:\\winrar-test\\ppmd\\");
				
				//str.append("server10.64.rar"); //modelorder 10 max 64mb				// >failed<
				//str.append("asdf4.rar"); //modelorder 2 max 2mb						// >failed<	

				//str.append("asdf6.rar"); //modelorder 2 max 2mb						// >ok<	
				//str.append("asdf7.rar"); //modelorder 2 max 2mb						// >ok<	
				//str.append("asdf8.rar"); //modelorder 2 max 2mb						// >ok<	
				
				//Delta
				//str.append("C:\\winrar-test\\delta\\");
				//str.append("server.rar"); //1 channel									// >ok<
				//str.append("server4c.rar"); //4 channels								// >ok<
				
				
				//Audio
				//str.append("C:\\winrar-test\\audio\\");
				//str.append("muh.rar"); 												// >ok< 
				//str.append("server.rar");//1 channel									// >ok<
				//str.append("server4c.rar");//4 channels								// >ok<
				//str.append("asdf5.rar");												// >ok<
				
				//True Color
				//str.append("C:\\winrar-test\\trueColor\\");
				//str.append("server.rar");												// >ok<
				//str.append("bmp.rar");												// >ok<
				
				
				//normal
				//str.append("C:\\winrar-test\\normal\\");
				//str.append("server.rar");												// >ok<
				//str.append("asdf.rar");												// >ok<
				//str.append("asdf3.rar");												// >ok<
				
				//solid
				//str.append("C:\\winrar-test\\solid\\");
				//str.append("solid2.rar");												// >ok<
				
				//multi volume
				//str.append("C:\\winrar-test\\multivol\\");
				//str.append("server.part01.rar");										// >ok but only the first part / file not complete<
				//str.append("server.part02.rar");										// >failed<
				
				//save only
				//str.append("C:\\winrar-test\\save\\");
				//str.append("server.rar");												// >ok<

				//multiple files not solid
				//str.append("C:\\winrar-test\\multiplefiles\\");
				//str.append("multifiles.rar");											// >ok<

				
				//encrypted
				str.append("C:\\winrar-test\\enc\\");
				//str.append("enc.rar");												// >failed< but recognized
				str.append("enc2.rar");												// >failed< but recognized
				//create file obj
				
				
				//bytearraytest
				//str.append("C:\\winrar-test\\");
				//str.append("brokenRar.rar");
				//str.append("blabla2.rar");											// >ok<
				//str.append("eclipse.rar");											// >ok<
				file = new File(str.toString());
				logger.info("trying to extract file: "+str.toString());
			}
			
			// create a readonlyfile object; this can be either a file or a byte array 
						
			ByteArrayOutputStream tempFile = new ByteArrayOutputStream();
			BufferedInputStream tempStream = new BufferedInputStream(new FileInputStream(file));
			while(true){
				try {
					int ch = tempStream.read();
					if(ch != -1){
						tempFile.write(ch);
					}else{
						break;
					}
				} catch (IOException e) {
					break;
				}
			}
//			ReadOnlyAccessFile f = new ReadOnlyAccessFile(file);
			ReadOnlyAccessByteArray f = new ReadOnlyAccessByteArray(tempFile.toByteArray());
			
			
			// try to read the archive
			Archive arc = null;
			try {
				arc = new Archive(f);
			} catch (RarException e) {
				logger.warn("archive consturctor error",e);
			}			
			
			arc.getFileHeaders().size();
			
			if(arc != null){
				if(arc.isEncrypted()){
					logger.warn("archive is encrypted cannot extreact");
					
				}
				
				// optional get all headers from archive later this can be used to get archive comments, recovery blocks etc
				List<BaseBlock> l = arc.getHeaders();
				
//				for (int i = 0; i < l.size(); i++) {
//					logger.info(l.get(i).getHeaderType());
//				}
				
				// get the file header from the archive this can be used to list the available files or to apply a file filter
				List<FileHeader> files = arc.getFileHeaders();
				
				// in this case we try to extract all files
				for(FileHeader fh : files)
				{
					if(fh.isEncrypted()){
						logger.warn("file is encrypted cannot extract: "+fh.getFileNameString());
						continue;
					}
					logger.info("extracting file:"+new String(fh.getFileNameByteArray()));
					logger.info("start: "+new Date());
					
					// create an output stream this can be what-so-ever output stream 
					// the file name can be aquired form the header
//					FileOutputStream os = new FileOutputStream(new String(fh.getFileNameByteArray()));
					ByteArrayOutputStream os = new ByteArrayOutputStream();
					
					
					try {
						arc.extractFile(fh, os);
					} catch (RarException e) {
						// TODO Auto-generated catch block
						logger.warn("error extracting file",e);
					}
					
					// if you're using byte arrays get the array from the stream 
					byte[] outMem = os.toByteArray();
					FileOutputStream fo = new FileOutputStream(new String(fh.getFileNameByteArray()));
					try {
						fo.write(outMem);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					logger.info("end: "+new Date());
				}
			}
			
		} catch (FileNotFoundException e) {
			logger.error("File not found!!!");
		}
		
	}


	
	
	
	


}















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -