public static void main(String args[]) throws FileNotFoundException, IOException{
ZipInputStream zis = new ZipInputStream(new FileInputStream ("c://a.zip"))//生成读取ZIP文件的流
ZipEntry ze = zis.getNextEntry()//取得下一个文件项
long size = ze.getSize()//取得这一项的大小
FileOutputStream fos = new FileOutputStream("c://"+ze.getName())//产生输出文件对象
for(int i= 0i<sizei++){//循环读取文件并写入输出文件对象
byte c = (byte)zis.read()
fos.write(c)
}
fos.close()
zis.close()
}
}
首先,要上传,必须建立一个标签并且在form中<input type="file" name="myFile">接收这个标签中的值的时候,你要在后台JAVA类中定义一个熟悉,比如:private File myFile
要是你用的servlet这样接收:myFile = getParmeter("myFile")
要是Struts1你要在一个ActionForm中定义private File myFile并且GET/SET
要是struts2你要在Action类中定义private File myFile并且GET/SET
OK现在是实现上传了代码如下(按照参数就传值):
/**
*
* @param file 文件
* @param max_length 最大
* @param allowed_types 类型
* @param path 路径
* @param fileName 文件名
* @param prName 前缀
* @return
*/
public String uploadFile(File file, Integer max_length, String allowed_types, String path, String fileName, String prName){
//这里是获取访问路径这里是struts2的方式,struts和servlet是另外中(好像是)
String root = ServletActionContext.getServletContext().getRealPath(path)
try {
if(file == null)
return ""
if(max_length <file.length() )
return ""
String fileExt = this.getFileExt(fileName)
if( ! allowed_types.contains(fileExt))
return ""
File filePath = new File(root)
if ( ! filePath.exists()) {
filePath.mkdirs()
}
//System.out.println(fileName)
fileName = prName + System.nanoTime() + "." + fileExt
String rootFileName = root + "/" + fileName
this.writeFile(file, rootFileName)
return path + "/" + fileName
} catch (Exception e) {
e.printStackTrace()
return ""
}
}
//以上就是把文件写入磁盘,也就是所谓的上传
下载压缩包解压的方法:
public void unZipFile() throws UnZipException{
try{
//ZipFile zipFile = new ZipFile(path)
BufferedOutputStream bos = null
FileInputStream fis = new FileInputStream(path)
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))
ZipEntry entry
while((entry = zis.getNextEntry()) != null){
byte[] data = new byte[BUFFER]
int length=0
//如果是文件夹就创建它
if(entry.isDirectory()){
String dirName=entry.getName()
String dirPath=targetPath+"/"+dirName
FileOperate fo = new FileOperate()
fo.createFolder(dirPath)
}
else{
FileOutputStream fos = new FileOutputStream(targetPath+"/"+entry.getName())
bos = new BufferedOutputStream(fos,BUFFER)
while((length=zis.read(data, 0, BUFFER)) != -1){
bos.write(data,0,length)
}
bos.flush()
bos.close()
fos.flush()
fos.close()
}
}
fis.close()
zis.close()
//zipFile.close()
}catch(ZipException e){
e.printStackTrace()
throw new UnZipException("不是有效的zip文件! "+e.getMessage())
}catch(IOException e){
e.printStackTrace()
throw new UnZipException("文件读取错误!"+e.getMessage())
}
}
import java.io.BufferedInputStreamimport java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStreamReader
import java.io.OutputStream
import java.util.ArrayList
import java.util.List
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
public class JZip {
public static int iCompressLevel//压缩比 取值范围为0~9
public static boolean bOverWrite//是否覆盖同名文件 取值范围为True和False
@SuppressWarnings("unchecked")
private static ArrayList AllFiles = new ArrayList()
public static String sErrorMessage
private String zipFilePath
public List<File>srcMap
public JZip () {
iCompressLevel = 9
//bOverWrite=true
}
public JZip(String zipFilePath) throws FileNotFoundException, IOException {
this.zipFilePath = zipFilePath
}
@SuppressWarnings("unchecked")
public static ArrayList extract (String sZipPathFile , String sDestPath) {
ArrayList AllFileName = new ArrayList()
try {
//先指定压缩档的位置和档名,建立FileInputStream对象
FileInputStream fins = new FileInputStream(sZipPathFile)
//将fins传入ZipInputStream中
ZipInputStream zins = new ZipInputStream(fins)
ZipEntry ze = null
byte ch[] = new byte[256]
while ((ze = zins.getNextEntry()) != null) {
File zfile = new File(sDestPath + ze.getName())
File fpath = new File(zfile.getParentFile().getPath())
if (ze.isDirectory()) {
if (!zfile.exists())
zfile.mkdirs()
zins.closeEntry()
} else {
if (!fpath.exists())
fpath.mkdirs()
FileOutputStream fouts = new FileOutputStream(zfile)
int i
AllFileName.add(zfile.getAbsolutePath())
while ((i = zins.read(ch)) != -1)
fouts.write(ch, 0, i)
zins.closeEntry()
fouts.close()
}
}
fins.close()
zins.close()
sErrorMessage = "OK"
} catch (Exception e) {
System.err.println("Extract error:" + e.getMessage())
sErrorMessage = e.getMessage()
}
AllFiles.clear()
return AllFileName
}
@SuppressWarnings({ "unchecked", "static-access" })
public static void compress (String sPathFile , boolean bIsPath , String sZipPathFile) {
try {
String sPath
//先指定压缩档的位置及档名,建立一个FileOutputStream
FileOutputStream fos = new FileOutputStream(sZipPathFile)
//建立ZipOutputStream并将fos传入
ZipOutputStream zos = new ZipOutputStream(fos)
//设置压缩比
zos.setLevel(iCompressLevel)
if (bIsPath == true) {
searchFiles(sPathFile)
sPath = sPathFile
} else {
File myfile = new File(sPathFile)
sPath = sPathFile.substring(0, sPathFile.lastIndexOf(myfile.separator) + 1)
AllFiles.add(myfile)
}
Object[] myobject = AllFiles.toArray()
ZipEntry ze = null
//每个档案要压缩,都要透过ZipEntry来处理
FileInputStream fis = null
BufferedReader in = null
//byte[] ch = new byte[256]
for (int i = 0 i <myobject.length i++) {
File myfile = (File) myobject[i]
if (myfile.isFile()) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(myfile.getPath()),"iso8859-1"))
//以档案的名字当Entry,也可以自己再加上额外的路径
//例如 ze=new ZipEntry("test\\"+myfiles[i].getName())
//如此压缩档内的每个档案都会加test这个路径
ze = new ZipEntry(myfile.getPath().substring((sPath).length()))
//将ZipEntry透过ZipOutputStream的putNextEntry的方式送进去处理
fis = new FileInputStream(myfile)
zos.putNextEntry(ze)
int len = 0
//开始将原始档案读进ZipOutputStream
while ((len = in.read()) != -1) {
zos.write(len)
}
fis.close()
zos.closeEntry()
}
}
zos.close()
fos.close()
AllFiles.clear()
sErrorMessage = "OK"
} catch (Exception e) {
System.err.println("Compress error:" + e.getMessage())
sErrorMessage = e.getMessage()
}
}
/*
这是一个递归过程,功能是检索出所有的文件名称
dirstr:目录名称
*/
@SuppressWarnings("unchecked")
private static void searchFiles (String dirstr) {
File tempdir = new File(dirstr)
if (tempdir.exists()) {
if (tempdir.isDirectory()) {
File[] tempfiles = tempdir.listFiles()
for (int i = 0 i <tempfiles.length i++) {
if (tempfiles[i].isDirectory())
searchFiles(tempfiles[i].getPath())
else {
AllFiles.add(tempfiles[i])
}
}
} else {
AllFiles.add(tempdir)
}
}
}
public String getZipFilePath() {
return zipFilePath
}
public void setZipFilePath(String zipFilePath) {
this.zipFilePath = zipFilePath
}
/**
* 解析zip文件得到文件名
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public boolean parserZip() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(zipFilePath)
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))
ZipEntry entry
try {
srcMap = new ArrayList<File>()
while ((entry = zis.getNextEntry()) != null) {
File file = new File(zipFilePath + File.separator + entry.getName())
srcMap.add(file)
}
zis.close()
fis.close()
return true
} catch (IOException e) {
return false
}
}
/**
*
* @param zipFileName 待解压缩的ZIP文件
* @param extPlace 解压后的文件夹
*/
public static void extZipFileList(String zipFileName, String extPlace) {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName))
File files = new File(extPlace)
files.mkdirs()
ZipEntry entry = null
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName()
if (entry.isDirectory()) {
File file = new File(files + entryName)
file.mkdirs()
System.out.println("创建文件夹:" + entryName)
} else {
OutputStream os = new FileOutputStream(files+File.separator + entryName)
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024]
int len
while ((len = in.read(buf)) >0) {
os.write(buf, 0, len)
}
os.close()
in.closeEntry()
System.out.println("解压文件:" + entryName)
}
}
} catch (IOException e) {
}
}
@SuppressWarnings("static-access")
public static void main(String args[]){
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)