问个问题,我想把字符串压缩成.zip,然后再解压.zip,解压之后是一个.txt文件。这个过程中,我不想先把字符串写入.txt,应该怎样实现?大家帮忙给段代码,谢谢!

解决方案 »

  1.   

    根据字符串返回字节数组写到zip里面
      

  2.   

    The_End_Of_The_World 最近很积极啊
      

  3.   

    这段代码是零前提直接生成一个包含带字符串文本文件的zip,较常见的应用,但不是向既存zip文件追加文本
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;public class AddFileToZip {    public static void main(String[] args) {
            appendFileToZip("test.zip");
        }    public static void appendFileToZip(String zipFileName) {
            try {
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                        zipFileName));
                // for put directory
                out.putNextEntry(new ZipEntry("/"));
                // for put file
                out.putNextEntry(new ZipEntry("test1.txt"));
                String stringIntoZipTxt = "This is the string put into the newly crreated text in the zip...";
                byte[] bs = stringIntoZipTxt.getBytes();
                out.write(bs);            System.out.println("Zip Done!");
                out.close();        } catch (Exception e) {
            }
        }}