ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName))
for(int i=0i<fileList.size()i++){
String filename = (String)fileList.get(i)
File file = new File(filename)
zip(out,file)
}
out.close()
下载的如下:
private int blockSize=65000
File file = new File(sourceFilePathName)
FileInputStream fileIn = new FileInputStream(file)
int readBytes = 0
readBytes = fileIn.read(b, 0, blockSize)
totalRead += readBytes
out.write(b, 0, readBytes)
代码大致如此,请参考。
向压缩包里添加文件时直接把服务器上的文件用流读进来就行,不用非把文件放到同一个目录,用程序生成压缩包和用命令行工具是不一样的,不要想当然。 写了个示例程序,你可以参考一下。这个示例不使用临时文件,把 OutputStream os替换成你下载用的输出流就可以实现一边压缩一边下载。注意java.util.zip不支持非ascii文件名。想支持中文文件名可以用apache ant或其他的库。import java.io.*
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
public class ZipTest {
public static void main( String[] args ) {
try {
writeZip()
} catch ( IOException e ) {
e.printStackTrace()
}
}
private static void writeZip() throws IOException {
String[] files = { "/ws/dir1/file1", "/ws/dir2/file2", "/ws/file3", "/pub/success.wav" }
OutputStream os = new BufferedOutputStream( new FileOutputStream( "/ws/archive.zip" ) )
ZipOutputStream zos = new ZipOutputStream( os )
byte[] buf = new byte[8192]
int len
for ( String filename : files ) {
File file = new File( filename )
if ( !file.isFile() ) continue
ZipEntry ze = new ZipEntry( file.getName() )
zos.putNextEntry( ze )
BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) )
while ( ( len = bis.read( buf ) ) >0 ) {
zos.write( buf, 0, len )
}
zos.closeEntry()
}
zos.close()
}
}
用的是Swing还是SWT?比较麻烦的,需要借助第三方工具如ex4j等,打包好后的exe差不多10M了(因为包含JRE运行库)
可以打包jar文件,jar文件可以在安装JRE的计算机上运行,不过大多电脑时没有安装的
Java这一点就不如C++、Delphi、C#了,C++、Delphi编译成功生成的就是exe,从win2000到win8一般都能良好运行,C#由于Vista之后微软系统自带.Net环境,所以很多电脑也能直接运行。
Java现在最擅长的是应该Web