📄 packbook.java
字号:
// 第n+1个图片的索引,(n+1张图片为虚拟的) Integer picN=curPos; System.out.println("==============================================="); System.out.println("125 lines curPos ==>"+curPos); /// 构建byteBuffer对象 ByteBuffer bb=ByteBuffer.allocate(curPos); bb.put(Tools.getByte4FromInt(curPos)); // 资源大小 bb.put(book.getId().getBytes()); // 书目id bb.put(bStory?"1".getBytes():"0".getBytes());// 是否小说: '1'表小说,'0'表杂志 if(bStory) // 连载为1,整本为0,杂志不用考虑,缺省为0 bb.put("c".equalsIgnoreCase(book.getSeriaProp())?"1".getBytes():"0".getBytes()); else bb.put("0".getBytes()); bb.put(Tools.getByte4FromInt(totalChapter)); // 书目总章节数 System.out.println("249 lines position ==>"+bb.position()+", 总章节数 ::"+totalChapter); bb.put(Tools.getByte4FromInt(allChapters)); // 打包的章节数 System.out.println("252 lines position ==>"+bb.position()+", 打包的章节数 ::"+allChapters); byte[] b =null; if(bStory){ b=buildBytes(book.getBookname().getBytes(),40); bb.put(b); // 正文书名 }else{ b=buildBytes(book.getBookname().getBytes(),20); bb.put(b); // 正文书名 b=buildBytes(("第"+book.getIssue()+"期").getBytes(),20); bb.put(b); // 正文书名 } System.out.println("158 lines position ==>"+bb.position()); // 添加打包的章节 String backChapter="("; if(chapters.length>1){ backChapter+=chapters[0]+"-"+chapters[chapters.length-1]; }else backChapter+=chapters[0]; backChapter+=")"; b=buildBytes((book.getBookname()+backChapter).getBytes(),40); bb.put(b); // 添加打包的章节 System.out.println("239 lines position ==>"+bb.position()+", 打包的章节 ::"+backChapter); // 添加章节号// for(int i=0;i<chapters.length;i++)// bb.put(Tools.getByte2FromInt(Integer.parseInt(chapters[i]))); int beginCH; // 打包的起始章节 int endCH; // 打包的结束章节 if(chapters.length>1){ beginCH=chapters[0]; endCH=chapters[chapters.length-1]; }else{ beginCH=endCH=chapters[0]; } System.out.println("286 lines position ==>"+bb.position()+", 开始章节 ::"+beginCH); bb.put(Tools.getByte4FromInt(beginCH)); bb.put(Tools.getByte4FromInt(endCH)); System.out.println("289 lines position ==>"+bb.position()+", 结束章节 ::"+endCH); // 添加封面索引,目录索引,各章节索引 for(FileInfo file:cpList){ System.out.println("291 lines 文件"+file.chapterNo+", 索引 ::"+file.indexPos); bb.put(Tools.getByte4FromInt(file.indexPos)); } System.out.println("294 lines position ==>"+bb.position()); // 添加 n+1章文件的索引 bb.put(Tools.getByte4FromInt(chapterN)); // 图片个数 bb.put(Tools.getByte2FromInt(picList.size())); // 添加图片名称 for(PictureInfo pi:picList){ bb.put(buildBytes(pi.picName,20)); } // 添加图片索引 for(PictureInfo pi:picList){ bb.put(Tools.getByte4FromInt(pi.indexPos)); } System.out.println("236 lines position ==>"+bb.position()); // 添加 n+1张图片的索引 bb.put(Tools.getByte4FromInt(picN)); // 添加封面数据,目录数据,各章节正文数据 for(FileInfo file:cpList){ //bb.put(Tools.getByte4FromInt(file.len)); // 文件长度 bb.put(file.data); // 文件数据 System.out.println("170 lines position ==>"+bb.position()+"文件"+file.chapterNo+" 索引==>"+file.indexPos+", 大小 ==>"+file.len); } // 图片数据 for(PictureInfo pi:picList){ //bb.put(Tools.getByte4FromInt(pi.size)); bb.put(pi.data); System.out.println("264 lines position ==>"+bb.position()+", 文件"+new String(pi.picName)+", 索引==>"+pi.indexPos+", 大小 ==>"+pi.size); } // 输出文件 // 打包dat文件 pack.dat // 开始写文件 FileOutputStream fos = null; // 文件输出对象 BufferedOutputStream bw=null; File file=null; try { file = new File(SOURCE_DIR+backName+".dat"); if (!file.exists()) file.createNewFile(); fos = new FileOutputStream(file); bw = new BufferedOutputStream(fos); writeFile(bb.array(),fos,bw); } catch (FileNotFoundException e) { System.err.println("文件不存在"); } catch (IOException e) { e.printStackTrace(); } } /** * 构造指定大小的byte数组 * @param by * @return */ private byte[] buildBytes(byte[] by,int len) { byte[] b=new byte[len]; for(int i=0;i<b.length;i++){ if(i<by.length){ b[i]=by[i]; } } return b; } /** * 解析字符串 * 图片标签:<IMG src="d:/www/book/3/2.png"> * @param fileStr * @param chList * @return */ public List<String> extraImg(String fileStr,List<String> picList) { System.out.println("Enter extraImg method"); String lch="<IMG"; String llch="<img"; String rch=">"; int ch=fileStr.indexOf(lch); if(ch<0) ch=fileStr.indexOf(llch); if(ch>=0){ String subStr=fileStr.substring(ch); ch=subStr.indexOf(rch); String chStr=subStr.substring(0, ch+1); picList.add(chStr); fileStr=subStr.substring(ch+1); return extraImg(fileStr,picList); }else return picList; } /** * //取图片的名称 * 从<IMG src="d:/www/book/3/2.png">,取src信息 * @param pic * @return */ public List<PictureInfo> buildPictureInfo(List<String> pic,String sourcePath){ System.out.println("Enter buildPictureInfo method parameter sourcePath ==>"+sourcePath); List<PictureInfo> picList=new ArrayList<PictureInfo>(); if(pic!=null&& pic.size()>0){ for(String str:pic){ int src=str.indexOf("src=\""); // 取 "src=""号的位置 if(src<0) src=str.indexOf("src='"); // 取 "src='"号的位置 str=str.substring(src+5); int ord=str.lastIndexOf("\""); // 取最后"""号的位置 if(ord<0) ord=str.lastIndexOf("'"); if(ord>0) str=str.substring(0, ord); String imgPath=str; int ch=imgPath.lastIndexOf("/"); PictureInfo p=new PictureInfo(); String name=""; if(ch<0) ch=imgPath.lastIndexOf("\\"); name=imgPath.substring(ch+1); System.out.println("394 lines img name ==>"+name); p.picName=name.getBytes(); System.out.println("405 imgPath ==>"+imgPath+", sourcePath ==>"+sourcePath); p.data=Tools.readFile2(sourcePath+imgPath); p.size=p.data.length; System.err.println(" Picture name ==>"+name+", data size ==>"+p.size); picList.add(p); // 添加图片数据信息 } } return picList; } /** * 封装文件数据信息 * @author jerry */ class FileInfo{ private int len; // 字符串长度 private byte[] data; // 字符串数据 private int chapterNo; // 章节号 private Integer indexPos;// 章节索引 } /** * 封装图片数据信息 * @author jerry */ class PictureInfo{ private int size; // 图片长度 private byte[] data; // 图片的数据 private byte[] picName; // 图片的名称 private Integer indexPos; // 图片数据索引 } /** * 将字节流写入文件 * @param filePath * @param buff * @param fos * @param bw */ public void writeFile( byte[] buff, FileOutputStream fos,BufferedOutputStream bw) { try { bw.write(buff, 0, buff.length); bw.flush(); fos.flush(); } catch (IOException e) { e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -