javazip压缩的文件怎么打开|java代码实现 导出zip包无法打开zip压缩包

|

A. 如何在java中解压zip和rar文件

java中有zip包,可以使用public void getZipFiles(String zipFile, String destFolder) throws IOException {BufferedOutputStream dest = null;ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));ZipEntry entry;while (( entry = zis.getNextEntry() ) != null) {System.out.println( "Extracting: " + entry.getName() );int count;byte data[] = new byte[BUFFER];if (entry.isDirectory()) {new File( destFolder + "/" + entry.getName() ).mkdirs();continue;} else {int di = entry.getName().lastIndexOf( '/' );if (di != -1) {new File( destFolder + "/" + entry.getName().substring( 0, di ) ).mkdirs();}}FileOutputStream fos = new FileOutputStream( destFolder + "/"+ entry.getName() );dest = new BufferedOutputStream( fos );while (( count = zis.read( data ) ) != -1) dest.write( data, 0, count );dest.flush();dest.close();}}rar的只能用第三方api,比如junrar

B. zip的文件,怎么解压缩打开

zip格式用手机怎么打开是很多朋友关注的话题,其实zip格式的文件是一种经过回压缩的文件,通过压缩之答后,文件的体积会变小,从而更有利于在网络上传播。经过压缩的文件,如果想要再次使用,就必须通过解压缩后才能使用,下面不妨就跟着小编一起去看看zip格式用手机如何打开吧。

以上就是ip格式用手机如何打开的具体介绍和操作方法了,希望可以帮助到你哦。

C. java压缩文件怎么解压

后缀名为.jar的文件不要解压,直接在java中运行就好了

D. java怎么读取Zip和RAR里面的文件啊

ZipInputStream是一个指向ZIP文件的流,这个流最重要的方法就是getNextEntry方法,一个zip文件可以包含好几个被压缩的文件,这个方法的功能就是返回下一个目录项,也就是返回zip文件中的下一项,并且把流指向这个目录文件项。getNextEntry的返回值是ZipEntry,它表示zip文件中的一个项,它可以返回这个文件项的大小、名称等。你可以根据它返回的文件大小调用ZipInputStream的read方法来读取需要的字节。给你一个例子:public class ZipTest { 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= 0;i<size;i++){//循环读取文件并写入输出文件对象 byte c = (byte)zis.read(); fos.write(c); } fos.close(); zis.close(); }}

E. java如何解压页面上传到服务器的zip文件

这个转换肯定是会出错的,struts 的formFile跟zipFile没有直接关系,怎么能这么强制转化呢?建议1. 把文件保存到一个临时目录(保存为zip文件)2. 读取这个文件3. 抽取想要的文件4. 把临时文件删除

F. java代码实现 导出zip包,无法打开zip压缩包

package com.lch.test;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;public class ZIP { public static void main(String[] argv) throws Exception { ZipFile zf = new ZipFile("E:\\wk\\LBSLEMIS201106141057\\LBSLEMIS\\test\\com\\lch\\test\\filename.zip"); for (Enumeration entries = zf.entries(); entries.hasMoreElements();) { String zipEntryName = ((ZipEntry) entries.nextElement()).getName(); System.out.println(zipEntryName); } }}用javad 的ZipFile类的ZipEntry方法试一下 找到ZIP里面的ZipEntry方法 读取Zip里面压缩文件的内容有可能会引用外包你好,我不知道你说的dzp是什么格式文件,但如果是zip的压缩文件,可以看下我的这段代码ZipFile file = new ZipFile("d:\\1.zip"); ZipEntry entry = file.getEntry("1.xml"); //假如压缩包里的文件名是1.xmlInputStream in=file.getInputStream(entry);最后就是按照java中一贯的流的处理方式即可 可以不解压,zip包里的一个对象就是一个ZipEntry找到你想要的那个ZipEntry,用文流写出来就可以了。追问通过ZipEntry,然后用流就可以读出里面的内容了吗?谢谢指点! 回答/** * 解压 * @param root 输出目标 * @param zipfile zip文件 */ protected void unzip(File root, File zipfile, String file) throws Exception { // 解压文件不存在时返回 if (!zipfile.exists()) { return; } // 释放目录不存时创建 if (!root.exists()) { root.mkdirs(); } // 释放目录不为目录时返回 if (!root.isDirectory()) { return; } FileInputStream fin = new FileInputStream(zipfile); ZipInputStream zin = new ZipInputStream(fin); ZipEntry entry = null; while ((entry = zin.getNextEntry()) != null) {// if (!entry.getName().endsWith(file)) {// continue;// } File tmp = new File(root, entry.getName()); if (entry.isDirectory()) { tmp.mkdirs(); } else { byte[] buff = new byte[4096]; int len = 0; tmp.getParentFile().mkdirs(); FileOutputStream fout = new FileOutputStream(tmp); while ((len = zin.read(buff)) != -1) { fout.write(buff, 0, len); } zin.closeEntry(); fout.close(); } } }这里完整的解压代码。// if (!entry.getName().endsWith(file)) {// continue;// }这段打开就是只解出一个你指定的文件。下面是测试用的。public static void main(String[] args) throws Exception {new CommonFiles().unzip(new File("D:\\"), new File("D:\\test.zip"),"file.txt");}这个例子会在D盘生成型个test文件夹,file.txt就会在里面,(里面也可能会有多个文件夹,这个取决于压缩包里文件的度)

G. 怎样用java快速实现zip文件的压缩解压缩

packagezip;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.util.Enumeration;importjava.util.zip.CRC32;importjava.util.zip.CheckedOutputStream;importjava.util.zip.ZipEntry;importjava.util.zip.ZipFile;importjava.util.zip.ZipOutputStream;importorg.apache.commons.lang3.StringUtils;publicclassZipUtil{ /** *递归压缩文件夹 *@paramsrcRootDir压缩文件夹根目录的子路径 *@paramfile当前递归压缩的文件或目录对象 *@paramzos压缩文件存储对象 *@throwsException */ privatestaticvoidzip(StringsrcRootDir,Filefile,ZipOutputStreamzos)throwsException { if(file==null) { return; } //如果是文件,则直接压缩该文件 if(file.isFile()) { intcount,bufferLen=1024; bytedata[]=newbyte[bufferLen]; //获取文件相对于压缩文件夹根目录的子路径 StringsubPath=file.getAbsolutePath(); intindex=subPath.indexOf(srcRootDir); if(index!=-1) { subPath=subPath.substring(srcRootDir.length()+File.separator.length()); } ZipEntryentry=newZipEntry(subPath); zos.putNextEntry(entry); BufferedInputStreambis=newBufferedInputStream(newFileInputStream(file)); while((count=bis.read(data,0,bufferLen))!=-1) { zos.write(data,0,count); } bis.close(); zos.closeEntry(); } //如果是目录,则压缩整个目录 else { //压缩目录中的文件或子目录 File[]childFileList=file.listFiles(); for(intn=0;n<childFileList.length;n++) { childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath()); zip(srcRootDir,childFileList[n],zos); } } } /** *对文件或文件目录进行压缩 *@paramsrcPath要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径 *@paramzipPath压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹 *@paramzipFileName压缩文件名 *@throwsException */ publicstaticvoidzip(StringsrcPath,StringzipPath,StringzipFileName)throwsException { if(StringUtils.isEmpty(srcPath)||StringUtils.isEmpty(zipPath)||StringUtils.isEmpty(zipFileName)) { thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL); } CheckedOutputStreamcos=null; ZipOutputStreamzos=null; try { FilesrcFile=newFile(srcPath); //判断压缩文件保存的路径是否为源文件路径的子文件夹,如果是,则抛出异常(防止无限递归压缩的发生) if(srcFile.isDirectory()&&zipPath.indexOf(srcPath)!=-1) { thrownewParameterException(ICommonResultCode.INVALID_PARAMETER,"."); } //判断压缩文件保存的路径是否存在,如果不存在,则创建目录 FilezipDir=newFile(zipPath); if(!zipDir.exists()||!zipDir.isDirectory()) { zipDir.mkdirs(); } //创建压缩文件保存的文件对象 StringzipFilePath=zipPath+File.separator+zipFileName; FilezipFile=newFile(zipFilePath); if(zipFile.exists()) { //检测文件是否允许删除,如果不允许删除,将会抛出SecurityException =newSecurityManager(); securityManager.checkDelete(zipFilePath); //删除已存在的目标文件 zipFile.delete(); } cos=newCheckedOutputStream(newFileOutputStream(zipFile),newCRC32()); zos=newZipOutputStream(cos); //如果只是压缩一个文件,则需要截取该文件的父目录 StringsrcRootDir=srcPath; if(srcFile.isFile()) { intindex=srcPath.lastIndexOf(File.separator); if(index!=-1) { srcRootDir=srcPath.substring(0,index); } } //调用递归压缩方法进行目录或文件压缩 zip(srcRootDir,srcFile,zos); zos.flush(); } catch(Exceptione) { throwe; } finally { try { if(zos!=null) { zos.close(); } } catch(Exceptione) { e.printStackTrace(); } } } /** *解压缩zip包 *@paramzipFilePathzip文件的全路径 *@paramunzipFilePath解压后的文件保存的路径 *@paramincludeZipFileName解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含 */ @SuppressWarnings("unchecked") publicstaticvoinzip(StringzipFilePath,StringunzipFilePath,booleanincludeZipFileName)throwsException { if(StringUtils.isEmpty(zipFilePath)||StringUtils.isEmpty(unzipFilePath)) { thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL); } FilezipFile=newFile(zipFilePath); //如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径 if(includeZipFileName) { StringfileName=zipFile.getName(); if(StringUtils.isNotEmpty(fileName)) { fileName=fileName.substring(0,fileName.lastIndexOf(".")); } unzipFilePath=unzipFilePath+File.separator+fileName; } //创建解压缩文件保存的路径 FileunzipFileDir=newFile(unzipFilePath); if(!unzipFileDir.exists()||!unzipFileDir.isDirectory()) { unzipFileDir.mkdirs(); } //开始解压 ZipEntryentry=null; StringentryFilePath=null,entryDirPath=null; FileentryFile=null,entryDir=null; intindex=0,count=0,bufferSize=1024; byte[]buffer=newbyte[bufferSize]; BufferedInputStreambis=null; BufferedOutputStreambos=null; ZipFilezip=newZipFile(zipFile); Enumeration<ZipEntry>entries=(Enumeration<ZipEntry>)zip.entries(); //循环对压缩包里的每一个文件进行解压 while(entries.hasMoreElements()) { entry=entries.nextElement(); //构建压缩包中一个文件解压后保存的文件全路径 entryFilePath=unzipFilePath+File.separator+entry.getName(); //构建解压后保存的文件夹路径 index=entryFilePath.lastIndexOf(File.separator); if(index!=-1) { entryDirPath=entryFilePath.substring(0,index); } else { entryDirPath=""; } entryDir=newFile(entryDirPath); //如果文件夹路径不存在,则创建文件夹 if(!entryDir.exists()||!entryDir.isDirectory()) { entryDir.mkdirs(); } //创建解压文件 entryFile=newFile(entryFilePath); if(entryFile.exists()) { //检测文件是否允许删除,如果不允许删除,将会抛出SecurityException =newSecurityManager(); securityManager.checkDelete(entryFilePath); //删除已存在的目标文件 entryFile.delete(); } //写入文件 bos=newBufferedOutputStream(newFileOutputStream(entryFile)); bis=newBufferedInputStream(zip.getInputStream(entry)); while((count=bis.read(buffer,0,bufferSize))!=-1) { bos.write(buffer,0,count); } bos.flush(); bos.close(); } } publicstaticvoidmain(String[]args) { StringzipPath="d:\ziptest\zipPath"; Stringdir="d:\ziptest\rawfiles"; StringzipFileName="test.zip"; try { zip(dir,zipPath,zipFileName); } catch(Exceptione) { e.printStackTrace(); } StringzipFilePath="D:\ziptest\zipPath\test.zip"; StringunzipFilePath="D:\ziptest\zipPath"; try { unzip(zipFilePath,unzipFilePath,true); } catch(Exceptione) { e.printStackTrace(); } }}

H. java se压缩文件下完之后怎么打开

你打开它打开winRAR的设置,把识别文件格式中的.jar给勾掉又是微软的东西多管闲事,去解压别人的可执行文件了。然后如果还不行重装jdk吧。

I. 怎么用Eclipse打开我朋友给我写的java程序 这个java程序是一个RAR压缩包 如何导入 新手求指导 详细点 谢

首先解压。

File->New->Java Project

不用填project,不要选择use default location,

然后browse是选择要导入的java项目;

如果只是java源文件的话,你就用Eclipse新建一个项目,然后把文件粘到项目的src文件夹下即可。

J. 如何在java中实现对zip和rar文件的解压

java中有包,可以使用

publicvoidgetZipFiles(StringzipFile,StringdestFolder)throwsIOException{BufferedOutputStreamdest=null;ZipInputStreamzis=newZipInputStream(newBufferedInputStream(newFileInputStream(zipFile)));ZipEntryentry;while((entry=zis.getNextEntry())!=null){System.out.println("Extracting:"+entry.getName());intcount;bytedata[]=newbyte[BUFFER];if(entry.isDirectory()){newFile(destFolder+"/"+entry.getName()).mkdirs();continue;}else{intdi=entry.getName().lastIndexOf('/');if(di!=-1){newFile(destFolder+"/"+entry.getName().substring(0,di)).mkdirs();}}FileOutputStreamfos=newFileOutputStream(destFolder+"/"+entry.getName());dest=newBufferedOutputStream(fos);while((count=zis.read(data))!=-1)dest.write(data,0,count);dest.flush();dest.close();}}

rar的只能用第三方api,比如junrar

https://github.com/edmund-wagner/junrar


赞 (0)