当前位置: 首页>>代码示例>>Java>>正文


Java ZipOutputStream.write方法代码示例

本文整理汇总了Java中org.apache.tools.zip.ZipOutputStream.write方法的典型用法代码示例。如果您正苦于以下问题:Java ZipOutputStream.write方法的具体用法?Java ZipOutputStream.write怎么用?Java ZipOutputStream.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.tools.zip.ZipOutputStream的用法示例。


在下文中一共展示了ZipOutputStream.write方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createZipOutputStream

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
@Test
@BenchmarkOptions(benchmarkRounds = 1, warmupRounds = 0, concurrency = 1)
public void createZipOutputStream() throws Exception {
	final String FILE_NAME = "custom/output/zipOutputStream.zip";
	//
	ZipOutputStream value = IoHelper.createZipOutputStream(FILE_NAME);
	System.out.println(value);
	assertNotNull(value);
	//
	String fileName = "custom/output/test.log";
	byte[] contents = IoHelper.read(fileName);
	ZipEntry zipEntry = new ZipEntry(fileName);
	value.putNextEntry(zipEntry);
	value.write(contents);

	// 須加 close,強制寫入
	IoHelper.close(value);
}
 
开发者ID:mixaceh,项目名称:openyu-commons,代码行数:19,代码来源:IoHelperTest.java

示例2: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
public static void zip(String path, List<File> files) throws IOException {
	ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(   
			new FileOutputStream(path), 1024));   
	for(File f : files) {
		String zipName = f.getName();
		if(!f.getName().contains(".png")) { 
			zipName = f.getName() + ".xml"; 
		}
		ZipEntry ze = new ZipEntry(zipName);
		ze.setTime(f.lastModified());
	    DataInputStream dis = new DataInputStream(new BufferedInputStream(   
	                new FileInputStream(f)));   
        zos.putNextEntry(ze);   
        int c;   
        while ((c = dis.read()) != -1) {   
            zos.write(c);   
        }   
	}
	zos.setEncoding("gbk");    
       zos.closeEntry();   
       zos.close();  
}
 
开发者ID:apicloudcom,项目名称:APICloud-Studio,代码行数:23,代码来源:ZipUtil.java

示例3: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
private void zip(ZipOutputStream out, File f, String base) throws Exception {
    if (f.isDirectory()) {
        File[] fl = f.listFiles();
        out.putNextEntry(new ZipEntry(base + File.separator));
        base = base.length() == 0 ? "" : base + File.separator;
        for (File fl1 : fl) {
            zip(out, fl1, base + fl1.getName());
        }
    } else {
        out.putNextEntry(new ZipEntry(base));
        FileInputStream in = new FileInputStream(f);
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        in.close();
    }
}
 
开发者ID:hou80houzhu,项目名称:rocserver,代码行数:19,代码来源:Jile.java

示例4: zipFile

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * <p>
 * 递归压缩文件
 * </p>
 *
 * @param parentFile parentFile
 * @param basePath basePath
 * @param zos zos
 */
private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception {
    File[] files = new File[0];
    if (parentFile.isDirectory()) {
        files = parentFile.listFiles();
    } else {
        files = new File[1];
        files[0] = parentFile;
    }
    String pathName;
    InputStream is;
    BufferedInputStream bis;
    byte[] cache = new byte[CACHE_SIZE];
    for (File file : files) {
        if (file.isDirectory()) {
            pathName = file.getPath().substring(basePath.length() + 1) + "/";
            zos.putNextEntry(new ZipEntry(pathName));
            zipFile(file, basePath, zos);
        } else {
            pathName = file.getPath().substring(basePath.length() + 1);
            is = new FileInputStream(file);
            bis = new BufferedInputStream(is);
            zos.putNextEntry(new ZipEntry(pathName));
            int nRead = 0;
            while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {
                zos.write(cache, 0, nRead);
            }
            bis.close();
            is.close();
        }
    }
}
 
开发者ID:JoyLau,项目名称:joylau-springboot-daemon-windows,代码行数:41,代码来源:ZipUtils.java

示例5: antzipFiles

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * @param file
 *            压缩文件
 * @param zipOutput
 *            ZipOutputStream
 * @param pathName
 *            相对路径
 * @throws Exception
 *             异常
 */
private static void antzipFiles(File file, ZipOutputStream zipOutput,
        String pathName) throws Exception {
    String fileName = pathName + file.getName();
    if (file.isDirectory()) {
        fileName = fileName + "/";
        zipOutput.putNextEntry(new ZipEntry(fileName));
        String fileNames[] = file.list();
        if (fileNames != null) {
            for (int i = 0; i < fileNames.length; i++) {
                antzipFiles(new File(file, fileNames[i]), zipOutput,
                        fileName);
            }
            zipOutput.closeEntry();
        }
    } else {
        ZipEntry jarEntry = new ZipEntry(fileName);
        BufferedInputStream in = new BufferedInputStream(
                new FileInputStream(file));
        zipOutput.putNextEntry(jarEntry);

        byte[] buf = new byte[bufSize];
        int len;
        while ((len = in.read(buf)) >= 0) {
            zipOutput.write(buf, 0, len);
        }
        in.close();
        zipOutput.closeEntry();
    }
}
 
开发者ID:545473750,项目名称:zswxsqxt,代码行数:40,代码来源:ZipFileUtils.java

示例6: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/** 递归压缩文件进zip文件流 */
private void zip(ZipOutputStream zos, File file, String base) throws IOException {
    if (file.isDirectory()) { // 目录时
        File[] files = file.listFiles();
        if (null != files && files.length > 0) {
            for (File f : files) {
                zip(zos, f, base + "/" + f.getName()); // 递归
            }
        } else {
            zos.putNextEntry(new ZipEntry(base + "/")); // 加入目录条目
            zos.closeEntry();
        }
    } else {
        zos.putNextEntry(new ZipEntry(base)); // 加入文件条目
        FileInputStream fis = new FileInputStream(file); // 创建文件输入流
        try {
            int count; // 读取计数
            byte[] buffer = new byte[Config.BUFFER_LENGTH]; // 缓冲字节数组
            /* 写入zip输出流 */
            while ((count = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, count);
            }
        } finally {
            zos.flush();
            zos.closeEntry();
            fis.close();
        }
    }
}
 
开发者ID:taugin,项目名称:cim,代码行数:30,代码来源:HttpDownHandler.java

示例7: zip

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * zip方法对文件或者文件夹进行压缩
 * 
 * @param zipstringPath
 *            为要压缩的文件或者文件夹路径
 * @param zipFileName
 *            为压缩之后生成的文件名
 */
public void zip(String zipstringPath, String zipFileName) throws Exception {
	try {

		byte[] buf = new byte[1024];
		File zipPath = new File(zipstringPath);
		int filelen = zipPath.listFiles().length;
		String[] filenames = new String[filelen];
		try {
			File[] files = zipPath.listFiles();
			for (int i = 0; i < filelen; i++) {
				filenames[i] = zipPath.getPath() + File.separator
						+ files[i].getName();
			}
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
					zipFileName));
			for (int i = 0; i < filenames.length; i++) {
				FileInputStream in = new FileInputStream(filenames[i]);
				out.putNextEntry(new ZipEntry(files[i].getName()));
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.closeEntry();
				in.close();
			}

			out.close();
		} catch (IOException e) {
			System.out.println(e);
		}

	} catch (Exception ex) {
		ex.printStackTrace(System.out);

	}
}
 
开发者ID:easyjf,项目名称:easyjweb,代码行数:45,代码来源:ZipUtils.java

示例8: addEntry

import org.apache.tools.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
 * 向 zip输出流 增加文件实体
 * 
 * @param zipEntryName
 *            zip中文件实体名称
 * @param file
 *            物理文件
 * @param zos
 *            ZipOutputStream
 * @param useSameDir
 *            值为 false 时,保存原有目录结构,zipEntryName 将被忽略
 * @throws IOException
 *             IO异常
 */
private static void addEntry(String zipEntryName, File file, ZipOutputStream zos,
        Boolean useSameDir) throws IOException {

    String entryName = zipEntryName;

    if (file.isDirectory()) {

        // 使用1层目录才可以用别名,否则还是原始文件名,且忽略目录
        if (useSameDir) {
            // File[] fl = file.listFiles();
            // for (int i = 0; i < fl.length; i++) {
            // addEntry(entryName,fl[i], zos,useSameDir);
            // }
        } else {
            entryName = getDirectoryPath(file) + "/";
            logger.debug("add zip entry directory: " + entryName);
            zos.putNextEntry(new ZipEntry(entryName));

            File[] fl = file.listFiles();
            for (int i = 0; i < fl.length; i++) {
                addEntry(entryName, fl[i], zos, useSameDir);
            }
        }

    } else {

        byte[] buf = new byte[BUFFER_SIZE];

        FileInputStream in = new FileInputStream(file);

        if (useSameDir) {
            entryName = zipEntryName;
        } else {
            entryName = getFilePath(file);
            // entryName = new File(entryName).getName();
        }

        ZipEntry entry = new ZipEntry(entryName);
        entry.setTime(file.lastModified());

        logger.debug("add zip entry file: " + entryName);

        zos.putNextEntry(entry);
        int len;
        while ((len = in.read(buf)) > 0) {
            zos.write(buf, 0, len);
        }
        in.close();
        zos.flush();
    }

    zos.closeEntry();
}
 
开发者ID:jiucai,项目名称:appframework,代码行数:68,代码来源:ZipFileUtil.java


注:本文中的org.apache.tools.zip.ZipOutputStream.write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。