社区
Java EE
帖子详情
急!!!把xml文件打包生成zip文件(100分)
adam8
2006-12-28 01:27:05
现在用代码已经生成了xml文件,求把xml文件再打包生成zip的压缩包的代码,或者其它压缩文件格式也可以
...全文
240
3
打赏
收藏
急!!!把xml文件打包生成zip文件(100分)
现在用代码已经生成了xml文件,求把xml文件再打包生成zip的压缩包的代码,或者其它压缩文件格式也可以
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
ttaallkk1
2006-12-28
打赏
举报
回复
强分
amozon
2006-12-28
打赏
举报
回复
// These are the files to include in the ZIP file
String[] filenames = new String[]{"filename1", "filename2"};
// Create a buffer for reading the files
byte[] buf = new byte[1024];
try {
// Create the ZIP file
String outFilename = "outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
// Compress the files
for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames[i]));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
} catch (IOException e) {
}
zxm_dgcstars
2006-12-28
打赏
举报
回复
程序代码及其注释
下列的程序实现了数据文件zip方式的压缩和解压缩方法。randomData()函数随机生成
50个double数据,并放在doc字符串变量中;openFile()函数读取ZIP压缩文件;saveFile()函数
将随机生成的数据存到ZIP格式的压缩文件中。
import java.util.zip.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.Math;
import java.io.*;
public class TestZip extends Frame implements ActionListener {
TextArea textarea; ∥显示数据文件的多行文本显示域
TextField infotip; ∥显示数据文件未压缩大小及压缩大小单行文本显示域
String doc; ∥存储随机生成的数据
long doczipsize = 0;∥压缩数据文件的大小
public TestZip(){
∥生成菜单
MenuBar menubar = new MenuBar();
setMenuBar(menubar);
Menu file = new Menu("File",true);
menubar.add(file);
MenuItem neww= new MenuItem("New");
neww.addActionListener(this);
file.add(neww);
MenuItem open=new MenuItem("Open");
open.addActionListener(this);
file.add(open);
MenuItem save=new MenuItem("Save");
save.addActionListener(this);
file.add(save);
MenuItem exit=new MenuItem("Exit");
exit.addActionListener(this);
file.add(exit);
∥随机生成的数据文件的多行文本显示域
add("Center",textarea = new TextArea());
∥提示文本原始大小、压缩大小的单行文本显示域
add("South",infotip = new TextField());
}
public static void main(String args[]){
TestZip ok=new TestZip();
ok.setTitle("zip sample");
ok.setSize(600,300);
ok.show();
}
private void randomData(){
∥随机生成50个double数据,并放在doc字符串变量中。
doc="";
for(int i=1;i<51;i++){
double rdm=Math.random()*10;
doc=doc+new Double(rdm).toString();
if(i%5 == 0) doc=doc+"n";
else doc=doc+" ";
}
doczipsize = 0;
showTextandInfo();
}
private void openFile(){
∥打开zip文件,将文件内容读入doc字符串变量中。
FileDialog dlg=new FileDialog(this,"Open",FileDialog.LOA D);
dlg.show();
String filename=dlg.getDirectory()+dlg.getFile();
try{
∥创建一个文件实例
File f=new File(filename);
if(!f.exists()) return; ∥文件不存在,则返回
∥用文件输入流构建ZIP压缩输入流
ZipInputStream zipis=new ZipInputStream(new FileInputStream(f));
zipis.getNextEntry();
∥将输入流定位在当前entry数据项位置
DataInputStream dis=new DataInputStream(zipis);
∥用ZIP输入流构建DataInputStream
doc=dis.readUTF();∥读取文件内容
dis.close();∥关闭文件
doczipsize = f.length();∥获取ZIP文件长度
showTextandInfo();∥显示数据
}
catch(IOException ioe){
System.out.println(ioe);
}
}
private void saveFile(){
∥打开zip文件,将doc字符串变量写入zip文件中。
FileDialog dlg=new FileDialog(this,"Save",FileDialog.SAVE);
dlg.show();
String filename=dlg.getDirectory()+dlg.getFile();
try{
∥创建一个文件实例
File f=new File(filename);
if(!f.exists()) return; ∥文件不存在,则返回
∥用文件输出流构建ZIP压缩输出流
ZipOutputStream zipos=new ZipOutputStream(new FileOutputStream(f));
zipos.setMethod(ZipOutputStream.DEFLATED); ∥设置压缩方法
zipos.putNextEntry(new ZipEntry("zip"));
∥生成一个ZIP entry,写入文件输出流中,并将输出流定位于entry起始处。
DataOutputStream os=new DataOutputStream(zipos);
∥用ZIP输出流构建DataOutputStream;
os.writeUTF(doc);∥将随机生成的数据写入文件中
os.close();∥关闭数据流
doczipsize = f.length();
∥获取压缩文件的长度
showTextandInfo();∥显示数据
}
catch(IOException ioe){
System.out.println(ioe);
}
}
private void showTextandInfo(){
∥显示数据文件和压缩信息
textarea.replaceRange(doc,0,textarea.getText().length());
infotip.setText("uncompressed size: "+doc.length()+"compressed size: "+dc zipsize);
}
public void actionPerformed(ActionEvent e){
String arg = e.getActionCommand();
if ("New".equals(arg)) randomData();
else if ("Open".equals(arg)) openFile();
else if ("Save".equals(arg)) saveFile();
else if ("Exit".equals(arg)){
dispose();∥关闭窗口
System.exit(0);∥关闭程序
}
else {
System.out.println("no this command!");
}
}
}
maven-3.0.5.
zip
maven-3.0.5.
zip
欢迎下载!
mac下apk反编译工具,包括apktool、dex2jar、jd-gui
mac下apk反编译工具,包括apktool、dex2jar、jd-gui
Axis2-1.6.2
Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物。是开发web service强大是工具软件。安装方法可参照博客“Axis2下载与安装”。
java导出多个
xml
文件
的压缩
zip
java导出多个
xml
文件
的压缩
zip
Web项目中使用java Struts2实现
Zip
、
xml
文件
的动态创建和下载(
Zip
文件
中动态存在多个
xml
文件
)
web项目中有时候会遇到动态
生成
zip
并下载的需求,但是在
生成
zip
文件
的时候我们又不希望产生冗余的临时
文件
。下面将 介绍我的处理方式。有不妥的地方还希望大家批评指教。 此次需求描述如下: 1.根据数据动态
生成
XML
文件
2.将动态
生成
的
XML
文件
(一个或多个)整合
打包
成一个
Zip
文件
3.客户通过浏览器点击后完成
Zip
文件
的下载 下面开始开发相应功能 1.根据数据动
Java EE
67,538
社区成员
225,852
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章