怎么不解压读写zip文件里的文件?

totti81 2002-03-05 09:22:41
帮帮忙 各位高手
...全文
230 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
totti81 2002-03-06
  • 打赏
  • 举报
回复
不好意思 我是想要在不完全解压的情况下向zip中的某个文件写入数据
skyyoung 2002-03-06
  • 打赏
  • 举报
回复
public String readZippedFile(String textFilename, String zipFilename) throws Exception
{
// Open a stream from the entry in the zip file for the file.
// Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
ZipFile zipFile;//zip file
ZipEntry zipEntry;//entry in zip
InputStream iStream;//input stream of file
try
{
zipFile = new ZipFile(zipFilename);
zipEntry = zipFile.getEntry(textFilename);
iStream = zipFile.getInputStream(zipEntry);
breader = new BufferedReader(new InputStreamReader(iStream));
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readZippedFile
mysine 2002-03-06
  • 打赏
  • 举报
回复
楼上说得对,给你一些具体代码
zipInputStream zin = new zipInputStream(new FileInputStream(zipname));
zipEntry entry;
while ((entry = zin.getNextEntry()) != null )
{
analyze entry;
read the contents of zinfile;
zin.closeEntry();
}
zin.close();
CanFly 2002-03-06
  • 打赏
  • 举报
回复


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;

public class ZipTest extends JFrame
implements ActionListener
{
private JComboBox fileList =new JComboBox();
private JTextArea fileText =new JTextArea();
private JMenuItem openItem,
exitItem;
private String zipname;
public ZipTest()
{
setTitle("ZipTest");
setSize(300,400);

JMenuBar mbar=new JMenuBar();
JMenu m=new JMenu("File");

openItem=new JMenuItem("Open");
openItem.addActionListener(this);
m.add(openItem);
exitItem=new JMenuItem("Exit");
exitItem.addActionListener(this);
m.add(exitItem);

mbar.add(m);

fileList.addActionListener(this);

Container contentPane=getContentPane();
contentPane.add(mbar,"North");
contentPane.add(fileList,"South");
contentPane.add(fileText,"Center");
}
public void actionPerformed (ActionEvent evt)
{
Object source=evt.getSource();
if (source==openItem)
{
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new
javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
return f.getName()
.toLowerCase()
.endsWith(".zip");
}
public String getDescription()
{ return "ZIP Files";}

});
int r=chooser.showOpenDialog(this);
if (r==JFileChooser.APPROVE_OPTION)
{
zipname=chooser.getSelectedFile()
.getPath();
scanZipFile();
}
}
else if (source==exitItem) System.exit(0);
else if (source==fileList)
loadZipFile((String)fileList.getSelectedItem());
}
public void scanZipFile()
{
fileList.removeAllItems();
try
{
ZipInputStream zin=new ZipInputStream(
new FileInputStream(zipname));
ZipEntry entry;
while ((entry=zin.getNextEntry()) !=null)
{
fileList.addItem(entry.getName()) ;
zin.closeEntry();
}
zin.close();
}
catch(IOException e){}
}
public void loadZipFile(String name)
{
try
{
ZipInputStream zin=new ZipInputStream(
new FileInputStream(zipname));
ZipEntry entry;
fileText.setText("");
while ((entry=zin.getNextEntry()) !=null)
{
if (entry.getName().equals(name))
{
BufferedReader in=new BufferedReader(
new InputStreamReader(zin));
String s;
while ((s=in.readLine())!=null)
{
fileText.append(s+"\n");
}
zin.closeEntry();
}
zin.close();
}
}
catch(IOException e){}
}
public static void main (String args[])
{
Frame f=new ZipTest();
f.show();
}
}
lucong 2002-03-05
  • 打赏
  • 举报
回复
Zip文件的操作可以看看java.util.zip
pengji 2002-03-05
  • 打赏
  • 举报
回复
你的代码!?有什么问题啊!?你不说别人怎么知道呢!?

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧