FileOutputStream out = new FileOutputStream("你好.zip"); CheckedOutputStream checkOut = new CheckedOutputStream(out,new Adler32()); ZipOutputStream zipOut = new ZipOutputStream(checkOut); BufferedOutputStream buff = new BufferedOutputStream(zipOut);
zipOut.setComment("A test of Java Zipping");
System.out.println("Writing file ");
long startTime = System.currentTimeMillis();
BufferedReader read = new BufferedReader(new FileReader("Filesnohup.txt")); // fileChannel = new FileInputStream("Filesnohup.txt").getChannel(); // access = new RandomAccessFile("Filesnohup.txt","rw"); // fileChannel = access.getChannel(); zipOut.putNextEntry(new ZipEntry("tb/RT/111.txt")); //设置在压缩文档里面的文件目录 int c ; // while(fileChannel.read(byteBuffer) != -1) while((c=read.read()) != -1) { buff.write(c); // byteBuffer.clear(); } buff.flush(); read.close(); read = new BufferedReader(new FileReader("demo.txt")); // fileChannel = new FileInputStream("demo.txt").getChannel(); // access = new RandomAccessFile("demo.txt","rw"); // fileChannel = access.getChannel(); zipOut.putNextEntry(new ZipEntry("111.txt")); // while(fileChannel.read(byteBuffer) != -1) while((c=read.read()) != -1) { buff.write(c); // byteBuffer.clear(); } buff.flush(); read.close();
System.out.println("Writing File Total Time :" + (System.currentTimeMillis()-startTime)); System.out.println("Writing finish "); // Checksum valid only after the file has been closed! System.out.println("Checksum: " + checkOut.getChecksum().getValue());
// Now extract the files: System.out.println("Reading file");
startTime = System.currentTimeMillis();
FileInputStream in = new FileInputStream("Filesnohup.zip"); CheckedInputStream checkIn = new CheckedInputStream(in,new Adler32()); ZipInputStream zipIn = new ZipInputStream(checkIn); BufferedInputStream buffIn = new BufferedInputStream(zipIn); ZipEntry zipEntry ; File file; File path;
public static void main(String []args){
}
package File.IO.ZIP;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;public class ZipCompress { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
RandomAccessFile access ;
byte [] byteArray = new byte[BSIZE];
FileChannel fileChannel ;
ByteBuffer byteBuffer ;
byteBuffer= ByteBuffer.allocate(BSIZE);
FileOutputStream out = new FileOutputStream("你好.zip");
CheckedOutputStream checkOut = new CheckedOutputStream(out,new Adler32());
ZipOutputStream zipOut = new ZipOutputStream(checkOut);
BufferedOutputStream buff = new BufferedOutputStream(zipOut);
zipOut.setComment("A test of Java Zipping");
System.out.println("Writing file ");
long startTime = System.currentTimeMillis();
BufferedReader read = new BufferedReader(new FileReader("Filesnohup.txt"));
// fileChannel = new FileInputStream("Filesnohup.txt").getChannel();
// access = new RandomAccessFile("Filesnohup.txt","rw");
// fileChannel = access.getChannel();
zipOut.putNextEntry(new ZipEntry("tb/RT/111.txt")); //设置在压缩文档里面的文件目录
int c ;
// while(fileChannel.read(byteBuffer) != -1)
while((c=read.read()) != -1)
{
buff.write(c);
// byteBuffer.clear();
}
buff.flush();
read.close(); read = new BufferedReader(new FileReader("demo.txt"));
// fileChannel = new FileInputStream("demo.txt").getChannel();
// access = new RandomAccessFile("demo.txt","rw");
// fileChannel = access.getChannel();
zipOut.putNextEntry(new ZipEntry("111.txt"));
// while(fileChannel.read(byteBuffer) != -1)
while((c=read.read()) != -1)
{
buff.write(c);
// byteBuffer.clear();
}
buff.flush();
read.close();
read = new BufferedReader(new FileReader("temp.txt"));
// fileChannel = new FileInputStream("temp.txt").getChannel();
// access = new RandomAccessFile("temp.txt","rw");
// fileChannel = access.getChannel();
zipOut.putNextEntry(new ZipEntry("tb/temp.txt"));
// while(fileChannel.read(byteBuffer) != -1)
while((c=read.read()) != -1)
{
buff.write(c);
// byteBuffer.clear();
}
buff.flush();
read.close();
buff.close();
System.out.println("Writing File Total Time :" + (System.currentTimeMillis()-startTime));
System.out.println("Writing finish ");
// Checksum valid only after the file has been closed!
System.out.println("Checksum: " +
checkOut.getChecksum().getValue());
// Now extract the files:
System.out.println("Reading file");
startTime = System.currentTimeMillis();
FileInputStream in = new FileInputStream("Filesnohup.zip");
CheckedInputStream checkIn = new CheckedInputStream(in,new Adler32());
ZipInputStream zipIn = new ZipInputStream(checkIn);
BufferedInputStream buffIn = new BufferedInputStream(zipIn);
ZipEntry zipEntry ;
File file;
File path;
// MappedByteBuffer mapped ;
// IntBuffer intBuffer ;
// ByteBuffer byteBuffer ;
// FileInputStream inFile ;
while((zipEntry = zipIn.getNextEntry())!=null)
{
System.out.println("Reading file " + zipEntry.getName());
file = new File(zipEntry.getName());
if(!file.exists()) //文件或目录不存在
{
path = file.getParentFile();
if(path == null) //file非目录
{
if(file.createNewFile());
// inFile = new FileInputStream(zipEntry.getSize());
}else
if(path.isDirectory()) //该目录存在
{
if(file.createNewFile());
}else //该目录不存在
{
if(path.mkdirs())
if(file.createNewFile());
};
}else
{
file.delete();
file = new File(zipEntry.getName());
}
access = new RandomAccessFile(file,"rw");
fileChannel = access.getChannel();
// mapped= fileChannel.map(FileChannel.MapMode.READ_WRITE, 0,ZipCompress.length);
// intBuffer = mapped.asIntBuffer();
while(buffIn.read(byteArray) != -1)
{
fileChannel.position(fileChannel.size()); //fileChannel end
fileChannel.write(ByteBuffer.wrap(byteArray));
byteArray = new byte[BSIZE];
}
// intBuffer.put(x);
fileChannel.close();
}
buffIn.close();
System.out.println("Reading File Total Time :" + (System.currentTimeMillis()-startTime));
}
public static final int length = 0x8FFF; // 128 Mb
private static final int BSIZE = 1024;
}
package File.IO.ZIP;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;using java.io.BufferedInputStream;
[code=LGP]
BAOKUO java.io.BufferedInputStream;
[/code]
zip解压缩到时候,要检查条目是否目录,如果是可以建立目录(也可以忽略)。
解压缩文件的时候,要检查目录结构,如果不存在,需要先创建好目录。另外,java.util.zip包的实现有些问题,不知道为什么,有些别的工具压缩的zip文件,它不能正确解开。应该使用apache的commons组件中的zip包来解压。当然,如果你连压缩也确保是使用java.util.zip,可能就没这个问题了。
慢慢想吧。解压不难,压缩的时候就有一些麻烦的地方。
我有我写的ZIP源码,但现在在公司,不然可以给你看一下。
public static void readWriteZip() throws Throwable
{
....
while((entry=zin.getNextEntry())!=null)
{
entryName=entry.getName();// --------------------- if (entry.isDirectory())
continue;// --------------------- ZipEntry ze=new ZipEntry(entryName);
zout.putNextEntry(ze); while((data=zin.read())!=-1)
{
zout.write(data);
}
//zout.closeEntry();
//zin.closeEntry();
}
zout.close();
zin.close();
} 另外,你的IllegalArgumentException,应该不是文件夹的问题,而是编码问题
你把你的src zip里面的文件夹,以及文件名字都用英文,运行一下试试看