代码: 压缩

public class Zip {

	private int zipSize;

	public void doZip() {
		FileOutputStream fos;
		ZipOutputStream zos;

		try {
			String zipTo = File.separatorChar + "temp.zip";
			fos = new FileOutputStream(zipTo);
			zos = new ZipOutputStream(fos);

			String pathFrom = File.separatorChar + "Letters";
			File file = new File(pathFrom);
			File[] files = file.listFiles();
			int fileSize = 0;
			zipSize = 0;

			for (int i = 0; i < files.length; i++) {
				String fileName = files[i].getPath();
				FileInputStream fis = new FileInputStream(fileName);
				fileSize = fis.available();
				zipSize += fileSize;
				byte content[] = new byte[fileSize];
				fis.read(content);
				zos.putNextEntry(new ZipEntry(fileName));
				zos.write(content);

				fis.close();

				System.out.println("---file: " + fileName + " (" + fileSize
						+ " Bytes)");
			}

			zos.close();
			fos.close();

			System.out.println(zipSize + " Bytes\nzip complete...");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Zip().doZip();
	}

}

 

使用winzip, winrar, 7-zip都能正常解压

 

- -!  但是这样压缩还是有点儿问题的

 

用java解压的时候... 也能正常解压

 

但是。。。 啥来着

 

好像是 ZipEntry.getSize() 返回值不正确,总是0 

 

 

你可能感兴趣的:(压缩)