Java Code Examples for java.util.zip.ZipOutputStream#setComment()

The following examples show how to use java.util.zip.ZipOutputStream#setComment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZipStreamDemo.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 7 votes vote down vote up
/**
 * 压缩一个文件
 */
public static void output1(String filepath, String zipfilepath) throws Exception {
    // 1.使用 File 类绑定一个文件
    // 定义要压缩的文件
    File file = new File(filepath);
    // 定义压缩文件名称
    File zipFile = new File(zipfilepath);

    // 2.把 File 对象绑定到流对象上
    InputStream input = new FileInputStream(file);
    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));

    // 3.进行读或写操作
    zipOut.putNextEntry(new ZipEntry(file.getName()));
    zipOut.setComment("This is a zip file.");
    int temp = 0;
    while ((temp = input.read()) != -1) { // 读取内容
        zipOut.write(temp); // 压缩输出
    }

    // 4.关闭流
    input.close();
    zipOut.close();
}
 
Example 2
Source File: BugReport.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void createBugReport(File reportFile, Throwable exception, String userMessage, Process process,
		String logMessage, File[] attachments) throws IOException {
	ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(reportFile));
	zipOut.setComment("RapidMiner bug report - generated " + new Date());
	write("message.txt", "User message", userMessage, zipOut);
	// XML gets encrypted if it contains passwords, they are of no use to us anyway
	write("_process.xml", "Process as in memory.", process.getRootOperator().getXML(false, EncryptionProvider.DEFAULT_CONTEXT), zipOut);
	if (process.getProcessLocation() != null) {
		try {
			String contents = process.getProcessLocation().getRawXML();
			write(process.getProcessLocation().getShortName(), "Raw process file in repository.", contents, zipOut);
		} catch (Throwable t) {
			write(process.getProcessLocation().getShortName(), "Raw process file in repository.",
					"could not read: " + t, zipOut);
		}
	}
	write("_log.txt", "Log message", logMessage, zipOut);
	write("_properties.txt", "System properties, information about java version and operating system", getProperties(),
			zipOut);
	write("_exception.txt", "Exception stack trace", getStackTrace(exception), zipOut);

	for (File attachment : attachments) {
		writeFile(attachment, zipOut);
	}
	zipOut.close();
}
 
Example 3
Source File: LocalStore.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
/**
 * Compress a text file using the ZIP compressing algorithm.
 * 
 * @param filename the path to the file to be compressed
 */
public static void zipCompress(String filename) throws IOException {
  FileOutputStream fos = new FileOutputStream(filename + COMPRESSION_SUFFIX);
  CheckedOutputStream csum = new CheckedOutputStream(fos, new CRC32());
  ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(csum));
  out.setComment("Failmon records.");

  BufferedReader in = new BufferedReader(new FileReader(filename));
  out.putNextEntry(new ZipEntry(new File(filename).getName()));
  int c;
  while ((c = in.read()) != -1)
    out.write(c);
  in.close();

  out.finish();
  out.close();
}
 
Example 4
Source File: LocalStore.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Compress a text file using the ZIP compressing algorithm.
 * 
 * @param filename the path to the file to be compressed
 */
public static void zipCompress(String filename) throws IOException {
  FileOutputStream fos = new FileOutputStream(filename + COMPRESSION_SUFFIX);
  CheckedOutputStream csum = new CheckedOutputStream(fos, new CRC32());
  ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(csum));
  out.setComment("Failmon records.");

  BufferedReader in = new BufferedReader(new FileReader(filename));
  out.putNextEntry(new ZipEntry(new File(filename).getName()));
  int c;
  while ((c = in.read()) != -1)
    out.write(c);
  in.close();

  out.finish();
  out.close();
}
 
Example 5
Source File: OldAndroidZipFileTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
static void createCompressedZip(OutputStream bytesOut) throws IOException {
    ZipOutputStream out = new ZipOutputStream(bytesOut);
    try {
        int i;

        for (i = 0; i < 3; i++) {
            byte[] input = makeSampleFile(i);
            ZipEntry newEntry = new ZipEntry("file-" + i);

            if (i != 1) {
                newEntry.setComment("this is file " + i);
            }
            out.putNextEntry(newEntry);
            out.write(input, 0, input.length);
            out.closeEntry();
        }

        out.setComment("This is a lovely compressed archive!");
    } finally {
        out.close();
    }
}
 
Example 6
Source File: OldAndroidZipStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private static void createUncompressedZip(ByteArrayOutputStream bytesOut) throws IOException {
    ZipOutputStream out = new ZipOutputStream(bytesOut);
    try {
        long[] crcs = {0x205fbff3, 0x906fae57L, 0x2c235131};
        int i;

        for (i = 0; i < 3; i++) {
            byte[] input = makeSampleFile(i);
            ZipEntry newEntry = new ZipEntry("file-" + i);

            if (i != 1)
                newEntry.setComment("this is file " + i);
            newEntry.setMethod(ZipEntry.STORED);
            newEntry.setSize(128 * 1024);
            newEntry.setCrc(crcs[i]);
            out.putNextEntry(newEntry);
            out.write(input, 0, input.length);
            out.closeEntry();
        }

        out.setComment("This is a lovely, but uncompressed, archive!");
    } finally {
        out.close();
    }
}
 
Example 7
Source File: OldAndroidZipStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private static void createCompressedZip(ByteArrayOutputStream bytesOut) throws IOException {
    ZipOutputStream out = new ZipOutputStream(bytesOut);
    try {
        int i;

        for (i = 0; i < 3; i++) {
            byte[] input = makeSampleFile(i);
            ZipEntry newEntry = new ZipEntry("file-" + i);

            if (i != 1)
                newEntry.setComment("this is file " + i);
            out.putNextEntry(newEntry);
            out.write(input, 0, input.length);
            out.closeEntry();
        }

        out.setComment("This is a lovely compressed archive!");
    } finally {
        out.close();
    }
}
 
Example 8
Source File: UnzipUtils.java    From imsdk-android with MIT License 6 votes vote down vote up
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment, FeedBackServcie.Callback callback)
        throws IOException {
    final int BUFF_SIZE = 2048;
    ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
            zipFile), BUFF_SIZE));
    int i = 0;
    for (File resFile : resFileList) {
        zipFile(resFile, zipout, "");
        if(callback != null){
            i++;
            callback.showFeedProgress(i,resFileList.size(), FeedBackServcie.FeedType.ZIP);
        }
    }
    zipout.setComment(comment);
    zipout.close();
}
 
Example 9
Source File: Zip.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * 批量压缩文件(夹)
 * 
 * @param resFileList
 *            要压缩的文件(夹)列表
 * @param zipFile
 *            生成的压缩文件
 * @param comment
 *            压缩文件的注释
 * @throws IOException
 *             当压缩过程出错时抛出
 */
public static void zipFiles(Collection<File> resFileList, File zipFile,
		String comment)
		throws IOException
{
	ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(
			new FileOutputStream(
					zipFile), BUFF_SIZE));
	for (File resFile : resFileList)
	{
		zipFile(resFile, zipout, "");
	}
	zipout.setComment(comment);
	zipout.close();
}
 
Example 10
Source File: TacCompressUtils.java    From tac with MIT License 5 votes vote down vote up
/**
 * compress
 *
 * @param srcFile
 * @param destFile
 * @throws Exception
 */
public static void compress(File srcFile, File destFile) throws Exception {

    // CRC32 check
    CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
        destFile), new CRC32());

    ZipOutputStream zos = new ZipOutputStream(cos);
    zos.setComment(new String("comment"));
    compress(srcFile, zos, BASE_DIR);

    zos.flush();
    zos.close();
}
 
Example 11
Source File: ZipUtil.java    From android-tv-launcher with MIT License 5 votes vote down vote up
/**
 * 批量压缩文件(夹)
 *
 * @param resFileList 要压缩的文件(夹)列表
 * @param zipFile 生成的压缩文件
 * @param comment 压缩文件的注释
 * @throws IOException 当压缩过程出错时抛出
 */
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
        throws IOException {
    ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
            zipFile), BUFF_SIZE));
    for (File resFile : resFileList) {
        zipFile(resFile, zipout, "");
    }
    zipout.setComment(comment);
    zipout.close();
}
 
Example 12
Source File: ZipUtils.java    From iMoney with Apache License 2.0 5 votes vote down vote up
/**
 * 批量压缩文件(夹)
 *
 * @param resFileList 要压缩的文件(夹)列表
 * @param zipFile     生成的压缩文件
 * @param comment     压缩文件的注释
 * @throws IOException 当压缩过程出错时抛出
 */
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
        throws IOException {
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
            zipFile), BUFF_SIZE));
    for (File resFile : resFileList) {
        zipFile(resFile, zos, "");
    }
    zos.setComment(comment);
    zos.close();
}
 
Example 13
Source File: ZipUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) throws IOException {
    ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), 1048576));
    for (File resFile : resFileList) {
        zipFile(resFile, zipout, "");
    }
    zipout.setComment(comment);
    zipout.close();
}
 
Example 14
Source File: ZipStreamDemo.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
/**
 * 压缩一个目录
 */
public static void output2(String dirpath, String zipfilepath) throws Exception {
    // 1.使用 File 类绑定一个文件
    // 定义要压缩的文件夹
    File file = new File(dirpath);
    // 定义压缩文件名称
    File zipFile = new File(zipfilepath);

    // 2.把 File 对象绑定到流对象上
    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
    zipOut.setComment("This is zip folder.");

    // 3.进行读或写操作
    int temp = 0;
    if (file.isDirectory()) { // 判断是否是文件夹
        File[] lists = file.listFiles(); // 列出全部文件
        for (int i = 0; i < lists.length; i++) {
            InputStream input = new FileInputStream(lists[i]);
            // 设置ZipEntry对象
            zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + lists[i].getName()));
            while ((temp = input.read()) != -1) {
                zipOut.write(temp);
            }
            input.close();
        }
    }

    // 4.关闭流
    zipOut.close();
}
 
Example 15
Source File: UnzipUtils.java    From imsdk-android with MIT License 5 votes vote down vote up
/**
 * 批量压缩文件(夹)
 *
 * @param resFileList 要压缩的文件(夹)列表
 * @param zipFile 生成的压缩文件
 * @param comment 压缩文件的注释
 * @throws IOException 当压缩过程出错时抛出
 */
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
        throws IOException {
    final int BUFF_SIZE = 2048;
    ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
            zipFile), BUFF_SIZE));
    for (File resFile : resFileList) {
        zipFile(resFile, zipout, "");
    }
    zipout.setComment(comment);
    zipout.close();
}
 
Example 16
Source File: AbstractASiCSignatureService.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void storeZipComment(final ASiCParameters asicParameters, final ZipOutputStream zos) {
	if (asicParameters.isZipComment()) {
		zos.setComment(ASiCUtils.MIME_TYPE_COMMENT + ASiCUtils.getMimeTypeString(asicParameters));
	}
}
 
Example 17
Source File: ZipCompress.java    From java-core-learning-example with Apache License 2.0 4 votes vote down vote up
private static void zipFiles(String[] fileNames)
        throws IOException {
    // 获取zip文件输出流
    FileOutputStream f = new FileOutputStream("test.zip");
    // 从文件输出流中获取数据校验和输出流,并设置Adler32
    CheckedOutputStream csum = new CheckedOutputStream(f,new Adler32());
    // 从数据校验和输出流中获取Zip输出流
    ZipOutputStream zos = new ZipOutputStream(csum);
    // 从Zip输出流中获取缓冲输出流
    BufferedOutputStream out = new BufferedOutputStream(zos);
    // 设置Zip文件注释
    zos.setComment("测试 java zip stream");
    for (String file : fileNames) {
        System.out.println("写入文件: " + file);
        // 获取文件输入字符流
        BufferedReader in =
                new BufferedReader(new FileReader(file));
        // 想Zip处理写入新的文件条目,并流定位到数据开始处
        zos.putNextEntry(new ZipEntry(file));
        int c;
        while ((c = in.read()) > 0)
            out.write(c);
        in.close();
        // 刷新Zip输出流,将缓冲的流写入该流
        out.flush();
    }
    // 文件全部写入Zip输出流后,关闭
    out.close();

    // 输出数据校验和
    System.out.println("数据校验和: " + csum.getChecksum().getValue());
    System.out.println("读取zip文件");
    // 读取test.zip文件输入流
    FileInputStream fi = new FileInputStream("test.zip");
    // 从文件输入流中获取数据校验和流
    CheckedInputStream csumi = new CheckedInputStream(fi,new Adler32());
    // 从数据校验和流中获取Zip解压流
    ZipInputStream in2 = new ZipInputStream(csumi);
    // 从Zip解压流中获取缓冲输入流
    BufferedInputStream bis = new BufferedInputStream(in2);
    // 创建文件条目
    ZipEntry zipEntry;
    while ((zipEntry = in2.getNextEntry()) != null) {
        System.out.println("读取文件: " + zipEntry);
        int x;
        while ((x = bis.read()) > 0)
            System.out.write(x);
    }
    if (fileNames.length == 1)
        System.out.println("数据校验和: " + csumi.getChecksum().getValue());
    bis.close();

    // 获取Zip文件
    ZipFile zf = new ZipFile("test.zip");
    // 获取文件条目枚举
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        // 从Zip文件的枚举中获取文件条目
        ZipEntry ze2 = (ZipEntry) e.nextElement();
        System.out.println("文件: " + ze2);
    }

}