谁能帮帮我,谢谢,分少了可以再给! 楼主想用JAVA对文件进行压缩?sorry!不知道up! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Runtime.getRuntime().exec("d:\jdk\jar filename"); fast_time(fast_time) 说的有道理:〉 各位大哥不要耍我了,我是想用java编程来实现对文件的压缩,各位帮帮忙啊 http://expert.csdn.net/Expert/topic/1898/1898066.xml?temp=.6156885 import java.io.*;import java.util.*;import java.util.zip.*;public class ZipCompress { /** * 对文件进行压缩操作 * @param strSourceFilePath 压缩文件的原文件路径 * @param strSourceFile 压缩文件的原文件的文件名 * @param strZipPath 压缩处理后的文件存放路径 * @param strZipFile 压缩处理后的文件名 * @return 压缩是否成功 */ public static boolean zipFile(String strSourceFilePath,String strSourceFile,String strZipPath,String strZipFile){ try { FileOutputStream f = new FileOutputStream(strZipPath+strZipFile+".zip"); ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));// for(int i = 0; i < args.length; i++) { System.out.println( "reading file " + strSourceFilePath+strSourceFile); DataInputStream in =new DataInputStream(new FileInputStream(strSourceFilePath+strSourceFile)); out.putNextEntry(new ZipEntry(strSourceFile)); int c; while((c = in.read()) != -1){ out.write(c); } in.close();// } out.close(); return true; } catch(Exception e) { e.printStackTrace(); } return false; } /** * 对数据流进行压缩处理 * @param source 要进行压缩处理的数据流 * @param strFileName 压缩文件的相对路径 * @param strZipPath 压缩处理后的文件路径 * @param strZipFile 压缩处理后的文件名 * @return 压缩处理是否成功 */ public static boolean zip(byte[] source,String strFileName,String strZipPath,String strZipFile){ try { FileOutputStream f = new FileOutputStream(strZipPath+strZipFile+".zip"); ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));// for(int i = 0; i < args.length; i++) {// System.out.println( "reading data " + source);// DataInputStream in =new DataInputStream(new FileInputStream(strSourceFilePath+strSourceFile)); out.putNextEntry(new ZipEntry(strFileName));// int c;// while((c = in.read()) != -1){ out.write(source);// }// in.close();// } out.close(); return true; } catch(Exception e) { e.printStackTrace(); } return false; } /** * 对文件进行解压缩处理 * @param strZipPath 解压缩的文件路径 * @param strZipFile 解压缩的文件名 * @param strSourceFilePath 解压缩处理后的文件存放路径 * @param strSourceFile 解压缩处理后的文件名 * @return 解压缩是否成功 */ public static boolean unizipFile(String strZipPath,String strZipFile,String strSourceFilePath,String strSourceFile){ try { FileInputStream f = new FileInputStream(strZipPath+strZipFile+".zip"); ZipInputStream in = new ZipInputStream(new DataInputStream(f));// for(int i = 0; i < args.length; i++) { System.out.println( "Writing file " + strSourceFilePath+strSourceFile); DataOutputStream out =new DataOutputStream(new FileOutputStream(strSourceFilePath+strSourceFile)); in.getNextEntry(); int c; while((c = in.read()) != -1){ out.write(c); } in.close();// } out.close(); return true; } catch(Exception e) { e.printStackTrace(); } return false; } /** * 对压缩文件进行解压缩处理 * @param strZipPath 压缩文件存放路径 * @param strZipFile 压缩文件的文件名 * @return 返回解压缩处理后的文件流 */ public static byte[] unizip(String strZipPath,String strZipFile){// StringBuffer buf=new StringBuffer(10000); byte[] buffer=null; try { FileInputStream f = new FileInputStream(strZipPath+strZipFile+".zip"); ZipInputStream in = new ZipInputStream(new DataInputStream(f)); System.out.println( "reading file " + strZipPath+strZipFile+".zip"); ZipEntry d=in.getNextEntry();// buffer=new byte[(int)d.getSize()]; int c;// c=in.read(buffer); byte[] by=new byte[1000]; String str; while((c=in.read(by)) != -1){ buffer=appendbytes(buffer,by,c); } in.close(); } catch(Exception e) { e.printStackTrace(); } return buffer; } public static void main(String[] args) { ZipCompress a=new ZipCompress(); try{ a.zip("你好".getBytes(),"aa.txt","d:/","aa"); }catch(Exception e){ e.printStackTrace(); } byte[] temp=a.unizip("d:/","huangzg"); System.out.println(new String(temp)); } /** * 对字节的追加操作 * @param a 原字节 * @param b 添加的字节 * @param length 添加字节的长度 * @return 添加字节处理后的字节 */ private static byte[] appendbytes(byte[] a,byte[] b,int length){ int lengtha=0; if(a!=null){ lengtha=a.length; } if(b==null||length<1){ return a; } byte[] c=new byte[lengtha+length]; for(int i=0;i<lengtha;i++){ c[i]=a[i]; } for(int j=0;j<length;j++){ c[j+lengtha]=b[j]; } return c; }} /** * 进行压缩文件的处理 * @param source * @return */ private boolean zipFile(byte[] source){ boolean success=false; try{ success= ZipCompress.zip(source,_fileName.concat(".xml"),_filePath,_fileName); }catch(Exception ex){ errMsg = "压缩文件的过程失败!"; ExcAct.doHandle(ex); } return success; }调用的时候像这样 /** * 进行压缩文件的处理 * @param source * @return */ private boolean zipFile(byte[] source){ boolean success=false; try{ success= ZipCompress.zip(source,_fileName.concat(".xml"),_filePath,_fileName); }catch(Exception ex){ errMsg = "压缩文件的过程失败!"; } return success; } abigfrog(千年精灵)(★JAVA★) ( )谢谢你 一道关于多线程的问题,求解答 求九九乘法表,看清要求 二级哈希映射算法 关于jtextfield和jbutton组合的问题 请问大家一个replaceALL()函数的问题 救命!这是怎么回是!请高手帮我看看! 一道关于异常的问题,谢谢 谁能告诉我AttributeSet到底是什么东西?? 关于java图书 如何在javax.swing.JDesktopPane+javax.swing.JInternalFrame中生成ToolBar? why Error : Can't read classes.zip? 怎么将带时间的日期变量,变成只有日期的日期变量
import java.util.*;
import java.util.zip.*;
public class ZipCompress { /**
* 对文件进行压缩操作
* @param strSourceFilePath 压缩文件的原文件路径
* @param strSourceFile 压缩文件的原文件的文件名
* @param strZipPath 压缩处理后的文件存放路径
* @param strZipFile 压缩处理后的文件名
* @return 压缩是否成功
*/
public static boolean zipFile(String strSourceFilePath,String strSourceFile,String strZipPath,String strZipFile){
try {
FileOutputStream f = new FileOutputStream(strZipPath+strZipFile+".zip");
ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));
// for(int i = 0; i < args.length; i++) {
System.out.println( "reading file " + strSourceFilePath+strSourceFile);
DataInputStream in =new DataInputStream(new FileInputStream(strSourceFilePath+strSourceFile));
out.putNextEntry(new ZipEntry(strSourceFile));
int c;
while((c = in.read()) != -1){
out.write(c);
}
in.close();
// }
out.close();
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
} /**
* 对数据流进行压缩处理
* @param source 要进行压缩处理的数据流
* @param strFileName 压缩文件的相对路径
* @param strZipPath 压缩处理后的文件路径
* @param strZipFile 压缩处理后的文件名
* @return 压缩处理是否成功
*/
public static boolean zip(byte[] source,String strFileName,String strZipPath,String strZipFile){
try {
FileOutputStream f = new FileOutputStream(strZipPath+strZipFile+".zip");
ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));
// for(int i = 0; i < args.length; i++) {
// System.out.println( "reading data " + source);
// DataInputStream in =new DataInputStream(new FileInputStream(strSourceFilePath+strSourceFile));
out.putNextEntry(new ZipEntry(strFileName));
// int c;
// while((c = in.read()) != -1){
out.write(source);
// }
// in.close();
// }
out.close();
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
} /**
* 对文件进行解压缩处理
* @param strZipPath 解压缩的文件路径
* @param strZipFile 解压缩的文件名
* @param strSourceFilePath 解压缩处理后的文件存放路径
* @param strSourceFile 解压缩处理后的文件名
* @return 解压缩是否成功
*/
public static boolean unizipFile(String strZipPath,String strZipFile,String strSourceFilePath,String strSourceFile){
try {
FileInputStream f = new FileInputStream(strZipPath+strZipFile+".zip");
ZipInputStream in = new ZipInputStream(new DataInputStream(f));
// for(int i = 0; i < args.length; i++) {
System.out.println( "Writing file " + strSourceFilePath+strSourceFile);
DataOutputStream out =new DataOutputStream(new FileOutputStream(strSourceFilePath+strSourceFile));
in.getNextEntry();
int c;
while((c = in.read()) != -1){
out.write(c);
}
in.close();
// }
out.close();
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
} /**
* 对压缩文件进行解压缩处理
* @param strZipPath 压缩文件存放路径
* @param strZipFile 压缩文件的文件名
* @return 返回解压缩处理后的文件流
*/
public static byte[] unizip(String strZipPath,String strZipFile){
// StringBuffer buf=new StringBuffer(10000);
byte[] buffer=null;
try {
FileInputStream f = new FileInputStream(strZipPath+strZipFile+".zip");
ZipInputStream in = new ZipInputStream(new DataInputStream(f));
System.out.println( "reading file " + strZipPath+strZipFile+".zip");
ZipEntry d=in.getNextEntry();// buffer=new byte[(int)d.getSize()]; int c;
// c=in.read(buffer);
byte[] by=new byte[1000];
String str;
while((c=in.read(by)) != -1){
buffer=appendbytes(buffer,by,c);
}
in.close(); } catch(Exception e) {
e.printStackTrace();
}
return buffer;
}
public static void main(String[] args) {
ZipCompress a=new ZipCompress();
try{
a.zip("你好".getBytes(),"aa.txt","d:/","aa");
}catch(Exception e){
e.printStackTrace();
}
byte[] temp=a.unizip("d:/","huangzg");
System.out.println(new String(temp));
} /**
* 对字节的追加操作
* @param a 原字节
* @param b 添加的字节
* @param length 添加字节的长度
* @return 添加字节处理后的字节
*/
private static byte[] appendbytes(byte[] a,byte[] b,int length){
int lengtha=0;
if(a!=null){
lengtha=a.length;
}
if(b==null||length<1){
return a;
}
byte[] c=new byte[lengtha+length];
for(int i=0;i<lengtha;i++){
c[i]=a[i];
}
for(int j=0;j<length;j++){
c[j+lengtha]=b[j];
}
return c;
}
} /**
* 进行压缩文件的处理
* @param source
* @return
*/
private boolean zipFile(byte[] source){
boolean success=false;
try{
success= ZipCompress.zip(source,_fileName.concat(".xml"),_filePath,_fileName);
}catch(Exception ex){
errMsg = "压缩文件的过程失败!";
ExcAct.doHandle(ex);
}
return success;
}调用的时候像这样 /**
* 进行压缩文件的处理
* @param source
* @return
*/
private boolean zipFile(byte[] source){
boolean success=false;
try{
success= ZipCompress.zip(source,_fileName.concat(".xml"),_filePath,_fileName);
}catch(Exception ex){
errMsg = "压缩文件的过程失败!";
}
return success;
}