Java中创建ZIP⽂件的⽅法
java创建zip⽂件的代码如下如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipDemo {
public static void main(String[] args) throws IOException {
File file = new File("test.zip");
if(!ists())
String string="test ZIP!";
byte[] buffer =Bytes();
FileOutputStream fOutputStream = new FileOutputStream(file);
ZipOutputStream zoutput = new ZipOutputStream(fOutputStream);
ZipEntry zEntry = new ZipEntry("");
zoutput.putNextEntry(zEntry);
zoutput.write(buffer);
zoutput.closeEntry();
zoutput.close();
}
}
补充:下⾯看下JAVA中创建ZIP⽂件,并向ZIP⽂件中写⼊并读取条⽬和数据
package File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipDemo {
public static void main(String[] args) throws IOException {java创建文件
// TODO Auto-generated method stub
File file = new File("test.zip");
if(!ists())
String string="my name is ysd!";
byte[] buffer =Bytes();
byte[] BufferInput =new byte[4096];
FileOutputStream fOutputStream = new FileOutputStream(file);
ZipOutputStream zoutput = new ZipOutputStream(fOutputStream);
ZipEntry zEntry = new ZipEntry("");
zoutput.putNextEntry(zEntry);
zoutput.write(buffer);
zoutput.closeEntry();
zoutput.close();
ZipInputStream zInputStream = new ZipInputStream(new FileInputStream(file));
ZipEntry zipEntry = null;
while((NextEntry()) != null)
{
System.out.Name());
System.out.println(new String(BufferInput));
zInputStream.closeEntry();
}
zInputStream.close();
}
}
总结
以上所述是⼩编给⼤家介绍的Java中创建ZIP⽂件的⽅法,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。