这里是对文件和数据流进行压缩和解压缩处理的方法,数据量大的话,你可以设置数据传输的byte,你可以用socket或者直接用封装好的tcp来进行网络传输,在你的服务器端进行write,在你的client端就read就行了。
import java.io.*;
import java.util.*;
import java.util.zip.*; 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));
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));
out.putNextEntry(new ZipEntry(strFileName));
out.write(source);
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));
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){
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(); int c;
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;
}
}
import java.io.*;
import java.util.*;
import java.util.zip.*; 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));
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));
out.putNextEntry(new ZipEntry(strFileName));
out.write(source);
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));
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){
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(); int c;
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;
}
}
解决方案 »
- 我定义了一个数组如题,在题中的元素20得不到统计,帮帮忙,我是要统计数组中每个元素出现的次数,并将该元素与出现次数输出
- Java基本类
- 请教Apache+Tomcat 重写转发的问题
- 一段小小代码的疑问,望初学者帮解答,谢谢
- 在UltraEdit的左侧树中删了文件后,回车站里没有,有办法恢复码?
- 关于interbase连接的问题
- 关于时间的处理问题
- 我怎么发现很多jsp中调用的.java文件里面的方法都是静态的啊??是懒呢,还是这样写比用对象调用好啊??
- Linux系统下, 关于乱码的问题.....(解决了可一另加分)
- 路人甲高手们请进:用JAVA采用什么样的算法来自动画出一个中、大型公司分布式网络环境的拓扑图???
- 请问怎么将一个文件的内容以JTree的形式显示?
- 用window.close()关闭窗口的问题
也就是怎样构造输入流。即ZipInputStream in = new ZipInputStream(new DataInputStream(f));应该怎样与ZipOutputStream联系起来!而不是从磁盘读取文件的方式得到输入流!谢谢!!!!!!!
import java.io.*;
import java.net.*;public class JabberServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
} import java.net.*;
import java.io.*;public class JabberClient {
public static void main(String[] args)
throws IOException {
// Passing null to getByName() produces the
// special "Local Loopback" IP address, for
// testing on one machine w/o a network:
InetAddress addr =
InetAddress.getByName(null);
// Alternatively, you can use
// the address or name:
// InetAddress addr =
// InetAddress.getByName("127.0.0.1");
// InetAddress addr =
// InetAddress.getByName("localhost");
System.out.println("addr = " + addr);
Socket socket =
new Socket(addr, JabberServer.PORT);
// Guard everything in a try-finally to make
// sure that the socket is closed:
try {
System.out.println("socket = " + socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
for(int i = 0; i < 10; i ++) {
out.println("howdy " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} finally {
System.out.println("closing...");
socket.close();
}
}
}
再次叩谢!