Java实现压缩一个目录下所有图片文件

None.gif import  java.io. * ;
None.gif
import  java.util. * ;
None.gif
import  java.util.zip. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * 打包照片文件
ExpandedBlockEnd.gif 
*/

None.gif
public   class  PhotoZip
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void main(String[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (args.length < 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.out.println(
"Usage: java PhotoZip photodir target.zip");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] buf = new byte[1024]; // 缓冲区
InBlock.gif
            File dir = new File(args[0]); // 照片目录
InBlock.gif
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(args[1])); // 压缩包流
InBlock.gif
            
InBlock.gif            String[] files 
= dir.list(); //列表照片目录下文件
InBlock.gif
            String fPath = dir.getCanonicalPath();
InBlock.gif            
if (!fPath.endsWith(File.separator))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fPath 
= fPath + File.separator;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
for(int idx = 0; idx < files.length; idx ++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (files[idx].length() >= 12)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        zos.putNextEntry(
new ZipEntry(files[idx].substring(08+ ".jpg")); // 统一文件名为8位+扩展名.jpg
InBlock.gif
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fPath + files[idx]));
InBlock.gif                        
while (bis.read(buf, 01024!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            zos.write(buf);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        bis.close();
InBlock.gif                        zos.closeEntry();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch (ZipException zexp)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        zexp.printStackTrace();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            zos.close();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception exp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            exp.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;

转载于:https://www.cnblogs.com/rookieport/archive/2006/02/11/328745.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值