java采用压缩解压缩方式修改word中内容或替换标签
2013-01-21 15:43
381 查看
/**
* 解压缩
* @throws IOException
*/
public String compressAndUn() throws IOException{
File file=new File(sourceWordAddress);//取得word文件
String dir=ServletActionContext.getRequest().getRealPath("")+"\\zipFile\\"; //取得要解压缩文件到的目录
FileInputStream inputStream=new FileInputStream(file);
ZipInputStream zipInputStream=new ZipInputStream(inputStream);
ZipEntry entry=null;
byte ch[]=new byte[256];
while((entry=zipInputStream.getNextEntry())!=null){
File zFile=new File(dir+entry.getName());
if(entry.isDirectory()){
if(!zFile.exists()){
zFile.mkdirs();
}
zipInputStream.closeEntry();
}else{
File fpath=new File(zFile.getParent());
if(!fpath.exists()){
fpath.mkdirs();
}
FileOutputStream outputStream=new FileOutputStream(zFile);
int i;
while((i=zipInputStream.read(ch))!=-1){
outputStream.write(ch, 0, i);
}
zipInputStream.closeEntry();
outputStream.close();
}
}
inputStream.close();
此时解压缩word文件后,会产生很多xml文件,位于word文件夹中的document.xml文件包含了word的内容及样式,对此xml利用dom4j进行修改或替换即可
ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream(new File(ServletActionContext.getRequest().getRealPath("")
+"\\targetFile\\test"+UUID.randomUUID().toString()+".docx"))); //要压缩文件的路径及名称
String root=ServletActionContext.getRequest().getRealPath("")+"\\zipFile\\";
String current=ServletActionContext.getRequest().getRealPath("")+"\\zipFile\\";
File rootFile=new File(root);
File currentFile=new File(current);
addAllFiles(zipOutputStream,rootFile,currentFile);
zipOutputStream.close();
return SUCCESS;
}
//压缩word解压缩产生的文件
private void addAllFiles(ZipOutputStream zipOutputStream, File current,File root) throws IOException {
byte buffer[]=new byte[4096];
int bytesIndex;
String entries[]=current.list();
for(int i=0;i<entries.length;i++){
File f=new File(current,entries[i]);
if(f.isDirectory()){
addAllFiles(zipOutputStream, f, root);
continue;
}
String relativePath=getRelativePath(current,root);
FileInputStream fileInputStream=new FileInputStream(f);
if(!relativePath.equals("")){
relativePath=relativePath+"/";
}
ZipEntry entry=new ZipEntry(relativePath+f.getName());
zipOutputStream.putNextEntry(entry);
while((bytesIndex=fileInputStream.read(buffer))!=-1){
zipOutputStream.write(buffer, 0, bytesIndex);
}
fileInputStream.close();
}
}
private static String getRelativePath(File currentFile,File rootFile){
int len=rootFile.getPath().length();
String rePath=currentFile.getPath().substring(len);
if(rePath.length()>0){
rePath = rePath.substring(1);
}
return rePath;
}
* 解压缩
* @throws IOException
*/
public String compressAndUn() throws IOException{
File file=new File(sourceWordAddress);//取得word文件
String dir=ServletActionContext.getRequest().getRealPath("")+"\\zipFile\\"; //取得要解压缩文件到的目录
FileInputStream inputStream=new FileInputStream(file);
ZipInputStream zipInputStream=new ZipInputStream(inputStream);
ZipEntry entry=null;
byte ch[]=new byte[256];
while((entry=zipInputStream.getNextEntry())!=null){
File zFile=new File(dir+entry.getName());
if(entry.isDirectory()){
if(!zFile.exists()){
zFile.mkdirs();
}
zipInputStream.closeEntry();
}else{
File fpath=new File(zFile.getParent());
if(!fpath.exists()){
fpath.mkdirs();
}
FileOutputStream outputStream=new FileOutputStream(zFile);
int i;
while((i=zipInputStream.read(ch))!=-1){
outputStream.write(ch, 0, i);
}
zipInputStream.closeEntry();
outputStream.close();
}
}
inputStream.close();
此时解压缩word文件后,会产生很多xml文件,位于word文件夹中的document.xml文件包含了word的内容及样式,对此xml利用dom4j进行修改或替换即可
ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream(new File(ServletActionContext.getRequest().getRealPath("")
+"\\targetFile\\test"+UUID.randomUUID().toString()+".docx"))); //要压缩文件的路径及名称
String root=ServletActionContext.getRequest().getRealPath("")+"\\zipFile\\";
String current=ServletActionContext.getRequest().getRealPath("")+"\\zipFile\\";
File rootFile=new File(root);
File currentFile=new File(current);
addAllFiles(zipOutputStream,rootFile,currentFile);
zipOutputStream.close();
return SUCCESS;
}
//压缩word解压缩产生的文件
private void addAllFiles(ZipOutputStream zipOutputStream, File current,File root) throws IOException {
byte buffer[]=new byte[4096];
int bytesIndex;
String entries[]=current.list();
for(int i=0;i<entries.length;i++){
File f=new File(current,entries[i]);
if(f.isDirectory()){
addAllFiles(zipOutputStream, f, root);
continue;
}
String relativePath=getRelativePath(current,root);
FileInputStream fileInputStream=new FileInputStream(f);
if(!relativePath.equals("")){
relativePath=relativePath+"/";
}
ZipEntry entry=new ZipEntry(relativePath+f.getName());
zipOutputStream.putNextEntry(entry);
while((bytesIndex=fileInputStream.read(buffer))!=-1){
zipOutputStream.write(buffer, 0, bytesIndex);
}
fileInputStream.close();
}
}
private static String getRelativePath(File currentFile,File rootFile){
int len=rootFile.getPath().length();
String rePath=currentFile.getPath().substring(len);
if(rePath.length()>0){
rePath = rePath.substring(1);
}
return rePath;
}
相关文章推荐
- 直接修改class文件内容即使是文本会导致App异常,正确方式是修改java再用生成的class替换掉原有的class
- 替换word模板中的字 相当于修改word内容了(保存了word原风格)
- webservice 优化性能的方法,以返回Dataset为例,当大数据量时采用先压缩的方式,调用再解压缩
- 将输入框内容进行修改及替换html标签
- Java 读取rtf模板内容然后替换内容,保存为word
- 点击按钮改变标签内容(采用lambda函数方式)
- webservice 优化性能的方法,以返回Dataset为例,当大数据量时采用先压缩的方式,调用再解压缩
- java poi组件 读取word文档 替换文档 内容 图片
- java/poi读取word,并替换word中的文本内容,向word中插入图片的操作
- Java&Xml教程(三)使用DOM方式修改XML文件内容
- 合并两个WORD文档,并且修改Word中标签的内容
- 使用java代码采用jacob的方式吧word文档转化为pdf格式
- 定义好word模板之后,使用java和jacob代码替换里面的指定的内容生成word
- java/poi读取word,并替换word中的文本内容,向word中插入图片的操作
- Java&Xml教程(三)使用DOM方式修改XML文件内容
- php、js、java的正则替换文本方式制作模板内容输出
- Java&Xml教程(三)使用DOM方式修改XML文件内容
- Java采用DOM方式递归遍历XML文件内容
- java POI 修改 读取 word 内容