「java打包解包」java解压压缩包

博主:adminadmin 2023-03-19 08:50:10 348

本篇文章给大家谈谈java打包解包,以及java解压压缩包对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中Socket如何实现数据包传输的打包和解包?

socket一般调用accept方法等待连接:

public class ServerThread extends Thread{

ServicePanel servicePanel = null;

ServerSocket serverSocket = null;

Socket socket = null;

public ServerThread(ServicePanel servicePanel,ServerSocket serverSocket) {

this.servicePanel = servicePanel;

this.serverSocket = serverSocket;

}

public void run(){

while (true) {

try {

socket = serverSocket.accept();//等待连接

new ReciveFromClient(servicePanel,socket).start();

} catch (IOException e) {

// e.printStackTrace();

break;

}

}

}

}

至于收包,发包是以流的形式传送的:

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

一般包里面的内容也就是被我们封装在某个类中,这个类要实现序列化,即实现Serializable接口;

发送方法那些就要自己写了~

java怎么理解自动打包和自动解包?

就是JAVA的8个基本类型在被放到需要一个引用类型的地方,就会帮你自动变成封装类

java里面打包与解包的问题

-128到127是一个内存池中,在int定义时开辟了该片内存空间,所有的int型的数据都会在这片内存中取值,所以,-128到127比较都是true,不在这个范围会另外新开辟内存空间,这样,他们指向的地址就是不一致的。

你说的打包和解绑其实在java中叫自动拆箱和装箱,更形象,Integer是对象类型的,int是简单类型,Integer在网络传输时不会出现异常,Integer是对象类型,默认是null值,符合我们的要求,在转换过程中又会自动转换

用java实现文件夹的打包和解包(解包时按原有的目录结构)

import java.io.*;

import java.util.*;

import java.util.zip.*;

public class Unzip {

public static final void copyInputStream(InputStream in, OutputStream out)

throws IOException

{

byte[] buffer = new byte[1024];

int len;

while((len = in.read(buffer)) = 0)

out.write(buffer, 0, len);

in.close();

out.close();

}

public static final void main(String[] args) {

Enumeration entries;

ZipFile zipFile;

if(args.length != 1) {

System.err.println("Usage: Unzip zipfile");

return;

}

try {

zipFile = new ZipFile(args[0]);

entries = zipFile.entries();

while(entries.hasMoreElements()) {

ZipEntry entry = (ZipEntry)entries.nextElement();

if(entry.isDirectory()) {

// Assume directories are stored parents first then children.

System.err.println("Extracting directory: " + entry.getName());

// This is not robust, just for demonstration purposes.

(new File(entry.getName())).mkdir();

continue;

}

System.err.println("Extracting file: " + entry.getName());

copyInputStream(zipFile.getInputStream(entry),

new BufferedOutputStream(new FileOutputStream(entry.getName())));

}

zipFile.close();

} catch (IOException ioe) {

System.err.println("Unhandled exception:");

ioe.printStackTrace();

return;

}

}

}

// These are the files to include in the ZIP file

String[] source = new String[]{"source1", "source2"};

// Create a buffer for reading the files

byte[] buf = new byte[1024];

try {

// Create the ZIP file

String target = "target.zip";

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));

// Compress the files

for (int i=0; isource.length; i++) {

FileInputStream in = new FileInputStream(source[i]);

// Add ZIP entry to output stream.

out.putNextEntry(new ZipEntry(source[i]));

// Transfer bytes from the file to the ZIP file

int len;

while ((len = in.read(buf)) 0) {

out.write(buf, 0, len);

}

// Complete the entry

out.closeEntry();

in.close();

}

// Complete the ZIP file

out.close();

} catch (IOException e) {

}

java打包解包的问题

Java的自动包装,是针对基本类型的操作与对象操作不同而进行的一种特殊处理方式。

以下是摘自《Thinking in Java》中文第四版第23页的一段话:

“基本类型具有的包装器类,使得可以在堆中创建一个非基本对象,用来表示对应的基本类型。”

从以上内容可以知道:

1、打包后的是对象

2、对象是在堆中,而不是在栈中

至于这么做的原因,主要还是要使得基本类型与对象操作的一致性。

关于java打包解包和java解压压缩包的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。